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

后端 未结 3 1047
梦毁少年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: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.

提交回复
热议问题