try and Finally giving exception with no return statement , but there is no exception when return statement is written in method

后端 未结 3 975
梦毁少年i
梦毁少年i 2021-01-29 12:16

Please explain why Exception comes in first program but not in second program.

1) without return statement in read method

class Example
{   
    public s         


        
相关标签:
3条回答
  • 2021-01-29 12:41

    The reason is given in the Java Language Specification rules governing the execution of try/finally blocks. Essentially

    • if the try block throws an exception E and the finally completes normally then the overall try/finally throws E
    • if the try block throws E but the finally does not complete normally, then E is ignored and the overall try/finally "completes abruptly" for the reason arising from the finally block

    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.

    0 讨论(0)
  • 2021-01-29 12:45

    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 reason S (and the throw of value V 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.

    0 讨论(0)
  • 2021-01-29 12:46

    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:

    1. If the finally block completes normally, then the try statement completes abruptly for reason R.
    2. If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

    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.

    0 讨论(0)
提交回复
热议问题