Hi, In the first example, control is not coming to return statement after finally block. But, In second example, control is coming to return statement as expected after finally block execution. Please advice if you know the reason.
In the first statement there is a DivideByZero Error so both catch and finally block gets executed.
while in the second case there is no exception so it returns and then executes the finally.
Finally is a block that is executed compulsarily after the try block.
in the first example an exception occurs at line 05 (Divide by zero).
In case of exception Java stops executing the try block
- the remaining instructions in the try block (after the point where exception has ocurred) are simply skipped - Java does not execute them.
Java executes an appriotriate catch block (if any), then finally block (if exists), and finishes executing the whole try-catch-finally block.
Java does not retry the instruction where exception has ocurred not continue executing instructions that are below this point. Read java tutorial to refresh basics about exception handling:
Joined: Sep 23, 2010
Your explanation is much better than documentation..Thanks Ireneusz and Jigar.