try, catch, finally, return order of execution - ultra-detailed explanation

Article from: Xue Xuan's blog

You can also view his other similar articles, but also let you have a receipt!

Code Example: http://www.cnblogs.com/lanxuezaipiao/p/3440471.html

in conclusion:

1, whether there is wood there is abnormal, 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.

note:

  • The basic types of modifications that do not affect finally return results. (Passed value)
  • Modified list, map, etc. custom reference types, the impact of returned results. (Transfer address) pass the object is addressed

But after the test does not affect the date type. A bit strange.

For example:

Situation. 1: the try {} the catch () {}} the finally {return;
apparently program execution sequence.

Case 2: the try {return;} the catch () {}} the finally {return;

  1. First try block return statement execution (arithmetic expression including a return statement), but does not return;
  2. All code execution finally statement
  3. The last execution in return try to return
  • After finally block the return statement is not executed, because the program has been in return try.

Situation. 3: the try {} catch () {return;}} the finally {return;
. 1, the program first performs try, if an exception is encountered performed catch block,

  • There is an exception:
    • Execution catch the return statement, but does not return
    • All code execution finally statement,
    • Finally, return to return the catch block execution. Finally block the return statement is not executed.
  • None Exceptions: executing the try and then finally return ... again

Situation. 4: the try {return;} the catch () {} the finally {return;}

  • The try block return statement (arithmetic expression including a return statement), but does not return;
  • Finally block is executed again,
  • Finally block is executed, there is return, returned here.

At this time, the return value of the finally block, is the value after the execution of the code

Situation. 5: the try {} the catch () {return;} the finally {return;}

  • Program execution catch block return statement (arithmetic expression including a return statement), but does not return;
  • Finally block is executed again,
  • Finally block is executed, there is return, returned here.

Situation. 6: try {return;} the catch () {return;} the finally {return;}
. 1, program execution try block return statement (arithmetic expression including a return statement), but does not return;

  • There is an exception:

    • Catch block return statement execution (arithmetic expression including a return statement), but does not return;
    • Finally block is executed again
    • Finally block is executed, there is return, returned here.
  • nothing unusual:

  • Finally block is executed again

  • Finally block is executed, there is return, returned here. .

final conclusion:

  • After the try or catch any of the return statement before returning, finally, if present, will first execute finally statement,
  • If you have finally return statement, then the program will return, so finally the return is sure to be a return,
  • The compiler to finally return implemented as a warning.

IMPORTANT:
from finally return loss can lead to abnormal reference: of Can use WE "return" in finally Block [Duplicate]

Reference: http://blog.csdn.net/kavensu/article/details/8067850

Published 142 original articles · won praise 44 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_40916641/article/details/101266912