Search...

Thursday, December 11, 2014

Exception in JAVA

Exception

Exception is the inefficiency of the computer system to execute the given statement. An unexpected result obtained from the program is known as Exception.



An Exception is a run time error occur due to any statement not solvable by the microprocessor. In this situation JVM creates an object of exception class.


Some typical causes errors: 

  • File System error (due to disk is full or it has been removed). 
  • Memory error (due to memory in correctly allocated, memory leaks) .
  • Network error (due to network down, URL does not exists). 
  • Calculation error ( i.e number divided by 0). 

Types of Exception: 1) Checked 2) Unchecked 


Checked: It occurs due to predefined method which throw an exception on their use. Checked exception are caught by JAVA compiler at compile time & a program caused checked exception, cannot compile. 





Unchecked: This exception occur due to the any of the unexpected reason at run time. Ex: memory leakage, number divide by zero etc. These exception are caught by JVM at run time & the program terminate abnormally. 


Sub Class of class Exception: 


1) NoSuchMethodError :- This exception is generated by the JVM due to the unavailability of main method in the specified class. Main method should be as public static void main (String s[]). 


2) ClassNotFound Exception :- Occur due to unavailability of class in the current program. 


3) ArrayIndexOutOfBound Exception :- Occur due to accessing of array element from invalid index position. 

In simple term, if we have store 3 number 1,2,3 in array index 0,1,2 & we are going to access 5th array index position (which is not available) then this exception occur. 


4) NullPointer Exception :- When we are going to access a property or method of an object using a reference variable that does not refer to any object. 

 Ex: class hello

           {
                int a;
                  public static void main (String s[])
                 {
                    hello arr[]=new hello[5];
                    arr[0]-new hello();
                     System.out.println(arr[1].a);
                  }
           } 

5) NumberFormat Exception :- Generated due to invalid parsing of data type into another data type. In the below example if we don`t write integer.parseint () then it gave NumberFormatException because we are going to store number in string s[].






6) StringIndexOutOfBound Exception :- Generated due to accessing of string element from invalid position.



No comments:

Post a Comment