Java interview questions try.catch statement in the execution order of return and finally statements?

When the finally and return in the try.catch statement in Java exist at the same time, will the finally statement be executed? If executed, what is the execution order?

in conclusion:

  1. Regardless of whether there is an exception, the code in the finally block will be executed;
  2. When there is return in try.catch, finally will still be executed;
  3. It is best not to include return in finally, otherwise the program will exit early and the return value is not the return value saved in try or catch.
  4. During execution, the return statement first writes the return value into memory, then stops and waits for the execution of the finally statement block, and then executes the next paragraph.
  5. As for whether the return value changes or not, when finally calls any variable API, the return value will be modified; when finally calls any immutable API, it has no effect on the return value.

For example:

1、try{}catch(){}return;

Execute and return in the normal order.

2、try{return;}catch(){}finally{}return;

The program executes the code before return in the try code block, then executes the finally block, and finally executes the return in the try, and the return after the finally will no longer be executed.

3、try{ } catch(){return;} finally{} return;

The program executes the code in the try code block,

  • No exception: execute the code in finally, and then execute return.
  • An exception occurs: execute the code before the return in the catch, then execute the code in the finally statement, and then execute the return in the catch, the return after the finally will no longer be executed.

4、try{return;}catch(){} finally{return;}

The program executes the code before the try code block, and then executes the finally statement block. Because there is a return in the finally, the return in the try will no longer be executed.

5、try{} catch(){return;}finally{return;}

The program executes the code in the try code block,

  • There is no exception, directly execute finally and execute the return in the finally execution ends;
  • If an exception occurs, execute the code before retrun in the catch code block, and then execute the code in the finally block. Since there is a return in the finally, it will return directly, so the return in the catch will no longer be executed.

6、try{ return;}catch(){return;} finally{return;}

The program executes the code before return in the try code block,

  • No exception: execute the statement in finally, and return to the end program directly because there is return in finally.
  • An exception occurs: the statement in the return in the catch block is executed, and then the finally statement is executed. Because there is a return in the finally, the end program is returned directly.

The final conclusion: before any return statement in try or catch, if finally exists, the finally statement will be executed first. If there is a return statement in finally, then the program will end.

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108339440