According to Java Language Specification, Section §14.20.2
A try statement with a finally block is executed by first executing the try block. Then there is a choice:
- If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
- If the finally block completes normally, then the try statement completes normally.
- If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S
If I interpret it correctly then after execution of try block finally is invoked, But how all this works and why I got the output,
public static int TestTryFinallyBlock()
{
int i =0;
try
{
i= 10; //Perform some more operation
return i;
}
finally
{
i = 40;
}
}
public static void main( String[] args )
{
int i1 = TestTryFinallyBlock(); //Here the output was 10 not 40
}
I want to know how this thing produced output 10.
Is that when try block is executed and return statement is encountered the output value is already pushed to stack, and then the finally block is executed
I know that return is encountered first then finally blocks runs so output is 10, but
How jvm interpret or how the try finally block is handled or converted by jvm?
Is that jvm uses GOTO section jump section to go to finally section or the stack is already maintained?