Conclusion 1, regardless of the occurrence of wood anomalies, finally block code will be executed, 2, when there is return in try and catch, finally still execute; 3, Finally, it is executed after the expression operation after return (at this time not returning the value after the operation, but first to save the value to return, the tube finally in the code, the return value will not change, still is the previously saved value), So the function return value is determined before finally execution; 4. Finally, it is best not to include return, otherwise the program exits prematurely, and the return value is not a returned value saved in a try or catch. Example: situation 1:try{} catch () {}finally{} return;
Obviously the program executes sequentially. Condition 2:try{return;} catch () {} finally{} return;
The program executes the code in the try block before return (including the expression operation in the return statement);
Then execute the finally block, and finally execute the try return;
The statement return after the finally block, because the program has been return in a try and is no longer executing. Condition 3:try{} catch () {return;} finally{} return;
The program executes a try first, and if an exception is encountered to execute a catch block,
Exception: Executes the code in the catch before return (including the expression operation in the return statement), and executes all the code in the finally statement.
The last execution of the catch block is return. Finally, the code at 4 is no longer executing.
No exception: Execute try and finally return. Condition 4:try{return;} catch () {} Finally{return;}
The program executes the code in the try block before return (including the expression operation in the return statement);
The finally block is executed again because there is a return in the finally block and exits early. Condition 5:try{} catch () {return;} Finally{return;}
The program executes the code in the Catch block before return, including an expression operation in the return statement;
The finally block is executed again because there is a return in the finally block and exits early. Condition 6:try{return;} catch () {return;} Finally{return;}
The program executes the code in the try block before return (including the expression operation in the return statement);
Exception: Executes the code in the Catch block before return (including the expression operation in the return statement);
The finally block is executed again because there is a return in the finally block and exits prematurely.
No exception: Then the finally block is executed because there is a return in the finally block and exits early. Final Conclusion:
A finally statement is executed before any return statement in a try or catch is executed, if finally exists.
If finally there is a return statement, then the program will return, so finally in the return is bound to be return,
The compiler implements the finally return to a warning. Here's a test program .
public class Finallytest
{public
static void Main (string[] args) {
System.out.println (new Finallytest (). Test ());;
}
static int test ()
{
int x = 1;
Try
{
x + +;
return x;
}
Finally
{
++x}
}
}
The result is 2.
Analysis:
In the try statement, when the return statement is executed, the result returned is ready, and at this point the program is transferred to finally execution.
Before you turn, try to store the result you want to return to a local variable that is different from X, and then remove the return result from it after you execute finally.
Therefore, even if the variable x is changed in Finally, it does not affect the return result.
It should use the stack to save the return value.