try, catch, finally the execution sequence

In a Java program, exception handling is an important technique, when the processing block includes a return statement, the

Execution order try, catch, finally as follows:

1, regardless of whether an abnormality occurs, the finally block of code will be executed;

2, when there is try and catch return, finally it will still be executed;

3, finally is after the return statement is executed, the return (value at this time and did not return after the operation performed before, but saved first value to return, no matter how kind of code finally, the values ​​will not return change, still the previously saved value), the function returns the value before finally execution has been identified;

4, finally, if included in the return, then the program will return here, rather than try or catch the return returns, the return value will not try to save or catch the return value.

to sum up:

When there is a return statement in the try or catch, before returning the results, we will first return results reservations, finally statement in the block and the next (if finally there is),
if there is finally a return statement, then finally in the program from direct return results, otherwise it will return to try or return results in the catch,
but finally the return is generally a compiler warning.

 

Reprinted from: https: //blog.csdn.net/xx326664162/article/details/50266995

Guess you like

Origin www.cnblogs.com/superwei001/p/12038393.html