Can anybody please look at the following code and explain me why it is printing "exception, finally, 5"?
public class
Test {
public static void main(
String[] str) {
System.out.println(new Test().method1());
}
int method1() {
int i = 0;
try {
method2();
System.out.println("int try");
return i;
}
catch (Exception e)
{
System.out.print("exception, ");
i = 5;
return i;
}
finally { System.out.print("finally, "); i = 2;}
}
void method2() { throw new NullPointerException(); }
}
I was under the impression that it would print "exception, finally, 2" as the finally block is executed at the end just before returning.