Wednesday, February 11, 2009

Java DefaultExceptionHandler

An Exception is an abnormal and unexpected and also unwanted event that disturbs the normal flow of the program.
Ex: FileNotFoundExceptionArithmaticException etc..
Default Exception Handling Mechanism in JAVA:
class Sample
{
public static void main(String a[])
{
doStuff();
}
public static void doStuff()
{
doMoreStuff();
}
public static void doMoreStuff()
{
System.out.println(“hai”);
System.out.println(10/0);
}
}



RTE: Exception in thread “main” Java.Lang.Arithematic Exception: /by zero
At Sample . doMoreStuff(Sample.java)
At Sample . doStuff(Sample.java:9)
At sample . main(Sample. java :5)
• If an exception is raised inside any method is r3esponsible for the creation of exception object. (here doMoreStuff() is responsible )
• The Exception object must contain the fallowing information.
1. Name of the Exception(Java.Lang.ArithematicException)
2. Description of the Exception (/ by zero)
3. The position at which the exception raised. (stack trace).
• Now the method handover that exception object to the JVM. JVM will come with that exception object and it will check, is there any exception object and it will check is there any exception handler inside that method abnormally and removes the corresponding entry from the stack.
• Now the method handover that exception object to the JVM. JVM will come with that exception object and it will check, is that any exception handler inside that method. If there is no exception handler then JVM blindly terminate that method abnormally and removes the corresponding entry from the stack.
• JVM will check for the caller(doStuff()) containing any exception handling code. If the caller also doesn’t contain any exception handling code, then JVM simply terminate that method also and remove the corresponding entry from the stack.
• This process will continue, until main() method. If the main() also doesn’t have any exception handling code then JVM terminates main() abnormally and handover the responsibility of the exception handling to default exception handler.
• Now Default Exception handler print the exception information to the end user
CommonExceptions and Errors:
Exceptions or errors may be thrown by either JVM(JVM Exceptions) or thrown explicitly by application developers or API programmers(programmatic exception).
1. NULLPOINTER EXCEPTION:
Class Sample
{
Static int[] a;
Static String name;
public static void main(String a[])
{
System.out.println(a.length);
}
}
System.out.println(a[o]); // null pointer exception (NPE)
System.out.println(name.length); //NPE
This is child class of RuntimeException. It is unchecked exception.
This is thrown by JVM when attempting to acess an object with a reference variable whose current value is null.
Ie., on null if we are applying any operation we will get NullPointerException.
2. STACK OVERFLOW ERROR:-
• This is child class of virtual machine error.
• It is unchecked
Throwable ----> Error ---> VMError ---> StackOverFlowError
Ex:class Sample
{
public static void m1()
{
m1();
}
public static void main(String a[]){ m1(); } // stack overflow error
}
StackOverflow error is thrown by JVM when a method recurses too deeply.
class Sample
{
Sample() { new Sample(); }
public static void main(String a[]) { Sample s= new Sample(); }//Exception in main thread stack overflow error
}
3. ARRAY INDEXOUTOF BOUNDS EXCEPTION:
• This is a child class of IndexOutOfBoundsExcption.
Throwable ---> Exception ---> RunTimeException ----> IndexOutOfBoundsException---> ArrayIndexOutOfBoundsException, StingIndexOutOfBoundsException
• It is an unchecked exception thrown by JVM when we are accessing an array element with
Invalid index.
Ex:
class Sample
{
public static void main(String a[])
{
Int[] a=new int[6];
System.out.println(a[5]); //0
System.out.println(a[7]); //RTE: ArrayIndexOutOfException
System.out.println(a[-2]); //----do----
System.out.println(a[-2.5]); //CTE: PLP req: int found:double
}
}
4. CLASS CAST EXCEPTION:
Class Sample
{
public static void main(String a[]))
{
String s=”anu”;
Object o=(Object)s;
String s1=(String)O;
System.out.println(“hai”);
}
}
• It is the child class of RTE
• It is unchecked exception, thrown by JVM when attempting to cast a reference variable to a type the fails to cast a reference variable to a type the faith the IS- A test.
----> Object o=new Object();
String s=(String)o; // Class Cast Exception
----> StringBuffer s1= (StringBuffer)s;

5. NOCLASS DEF FOUND ERROR:

  • It is the child class of error and it is unchecked one and unreachable.
  • It is thrown by JVM. If the JVM or class loader tries to load the definition of the class and no definition of the class could be found.
6. EXCEPTION IN INITIALIZER ERROR:
class Sample
{
Static String s;
String s1=new String(s);
public static void main(String a[])
{
System.out.println(“hai”);
}
}//Exception in initializer error caused by java.lang.NullPointerException.
Thrown by the JVM to indicate that an exception is occur during initialization of static variable or initialization block.
7. ILLEGAL ARGUMENT EXCEPTION:
  • It extends RTE and unchecked.
  • Thrown by aPI developer to indicate that a method has been passed an illegal or inappropriate argument.
Ex: Thread class setPriority() and sleep() methods throw this exception.
class Sample
{
p s v m(String a[])
{
Thread.currentThread().setPriority(15);
}
}//CTE: Illegal Argument exception
8. NumberFormatException:
Integer i=new Integer(“10”);//valid
Integer i=new Integer(“ten”);//invalid
CTE: NumberFormateException
int i=Integer.parseInt(arg[0]);
In commandline if we give “10” is valid. But “ten” is invalid.
It is the direct child class of illegal argument exception and thrown programmatically to indicate the application has attempted to convert a string to the number, but the string doesn’t have appropriate format.
9. IllegalStateException:
It extends RTE and indicates that a method has been invoked at an illegal or inappropriate time.
Ex: PrintWriter out=res.getWriter();
Out.println(…);
Out.close();
Out.println(“…”); ---> this is not appropriate time to call print() on out object and hence throws IllegalStateException.
10. Assertion Error:
It is the child class of error throws programatically when assert statement when Assert statement fails.
Summarization:
Exception or error --------------------- thrown by
1. NullPointerException ------------------ JVM
2. StackOverFlowErrorException --------------- JVM
3. ArrayIndexOutOfBoundsException ------------- JVM
4. ClassCastException ------------------------- JVM
5. NoClassDefFoundError -------------------- JVM
6. ExceptionInIntializerError -------------------- JVM

thrown by programmatically by programmer API developer:
7. IllegealArgumentException
8. NumberFormatException
9. IllegalStateException
10. Assertion Error

0 comments:

Post a Comment