Sunday, June 10, 2012

why the code after final not executes in this case


package com.yagapp.phaseone;


public class Testing2 {


/**
* @param args
*/
public static void main(String[] args) {
int y=0;
try{
int x =2/y;
}
catch(RuntimeException e)
{
System.out.println(1);
int z=2/y;

}
catch(Exception e){
System.out.println(2);
int z=2/y;
}
finally{
System.out.println(3);
}
System.out.println(4);
}


}


in this our output will give the result of 1 and 3 only
you won't see the result 4 
why ?
here is the reason

see in the code in the red color will throw the exception , but for this exception  code their is no handler provided for this exception  in this class , so on reaching the finally code and executing the code inside the finally code JVM calls the , ThreadGroup class methode  

private void dispatchUncaughtException(Throwable e) {
        getUncaughtExceptionHandler().uncaughtException(this, e);
    } ,
 so u won't see the result 4 


No comments:

Post a Comment