Please explain why Exception comes in first program but not in second program.
1) without return statement in read method
class Example
{
public s
The reason is given in the Java Language Specification rules governing the execution of try/finally blocks. Essentially
An explicit return
is considered abrupt rather than normal completion, so in your example 2 the return inside the finally masks the divide-by-zero exception raised by the try.
In both instance, the code with throw a java.lang.ArithmeticException
, however the return
in the finally
discards the exception and instead the method exits without forwarding the exception to the caller.
This is one of the reason why in general you should not use a return
in a finally
-block.
This is described in the Java Language Specification (8), 14.20.2 Execution of try-finally and try-catch-finally:
If execution of the try block completes abruptly because of a throw of a value
V
, then there is a choice:... (some similar but for this case irrelevant rules left out)
If the run-time type of
V
is not assignment compatible with a catchable exception class of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
If the finally block completes normally, then the try statement completes abruptly because of a throw of the value
V
.If the finally block completes abruptly for reason
S
, then the try statement completes abruptly for reasonS
(and the throw of valueV
is discarded and forgotten).
You can find the definition of abrupt and normal completion in JLS 14.1 Normal and Abrupt Completion of Statements. But basically a return
is considered an abrupt completion.
Branching statements(return ,goto ) should no be used inside finally because execution of such statement nullifies other instructions which are executed before finally.
The Java Language Specification says : If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and then there is a choice:
Note - A return statement inside a finally block will cause any exception that might be thrown in the try or catch block to be discarded.