Hi
Take this example...
public class DebugFinally {
public static void main(String[] args) {
try{
int number = Integer.parseInt("1245");
}catch(Exception ex){
System.out.println("In Catch");
return;
}finally{
System.out.println("In Finally");
}
System.out.println("Not Returned");
}
}
Output:
In Catch
In Finally
As you see above the program performed what was written in Catch but
after that it directly goes to finally block and execute what ever is
written in it, after that it comes back to the catch block to call the
return.
Basically internally the compiler executes whatever is written in the
finally block before executing any statement which takes the control
out of try/catch construct.
But, there is one exception to this. If I call System.exit(0); in place
of return then, program would end abruptly without giving care to
whatever is written in finally block.
And in that scenario the output would be modified as follows:
}catch(Exception ex){
System.out.println("In Catch");
System.exit(0);
}finally{
System.out.println("In Finally");
}
Output:
In Catch