Important Conclusions:
1. The code in the finally block executes whether or not an exception occurs
2. When there is a return in the try and catch, finally will still execute
3.finally is performed after the expression operation after return (at this point the value of the operation is not returned, but the value to be returned is saved, regardless of the code in the Finally, the returned value will not change, it is still the value saved before), That is, the function return value is determined before the finally execution
It is best not to include return in 4.finally, otherwise the program exits prematurely and the return value is not the return value saved in a try or catch
Example Analysis:
Case 1: try{} catch () {}finally{} return;
The program executes sequentially.
Case 2: Try{return;} catch () {} finally{} return;
Code that the program executes before return in the try block, including expression operations in the return statement,
Then execute the finally block and finally execute the return in try;
Because the program has already return in try, the return statement after the finally block is no longer executed.
Case 3: try{} catch () {return;} finally{} return;
The program executes the try first, and if an exception is encountered to execute a catch block,
Exception: code that executes before return in a catch, including expression operations in a return statement,
Then execute all the code in the finally statement, and finally execute the return in the Catch block,
The return statement after finally is no longer executed.
No exception: After executing the try and finally return
*case 4 : Try{return;} catch () {} Finally{return;}
Code that the program executes before return in the try block, including expression operations in the return statement,
then execute the finally block, because there is a return in the finally block and exit early.
*case 5: try{} catch () {return;} Finally{return;}
Code that the program executes before return in the catch block, including the expression operation in the return statement,
then execute the finally block, because there is a return in the finally block and exit early.
*case 6: try{return;} catch () {return;} Finally{return;}
Code that the program executes before return in the try block, including expression operations in the return statement,
Exception: code that executes before return in the catch block, including expression operations in the return statement,
Then execute the finally block, because there is a return in the finally block and exit early.
No exception: The finally block is executed, because there is a return in the finally block and exits early.
* Note: Because the C # compiler does not allow a return statement in Finally, Case 4, Case5, and case6 apply only to the Java language (there will be a warning)
Final conclusion:
If a finally statement block exists before executing a return statement in a try or catch, the statement in finally is executed, and if finally there is a return statement, the program will return directly. This causes the return value to be not the return value saved in the try or catch, which is implemented by the Java compiler as a warning, whereas in C # it is not allowed to use the return statement in finally.
In the case of a return statement, the order of execution of the try-catch-finally