In Java, when try and finally statements contain return statements, what is the execution situation? Whether the code in finally is executed, there are many different opinions. Some say it can execute, and some say it can not execute. Which statement is correct? Now let’s explain it with the following example:
The first case: the try contains the return statement, and finally does not contain it.
public class TestTry { static String s=""; public static void main(String args[]){ s = test1(); System.out.println("8 "+s); } public static String test1(){ try { System.out.println("try......"); return s = "a"; } finally{ s="b"; System.out.println("17 "+s); } } }Here we define a string s, assign "a" to s in try, and directly return it, and assign "b" to s in finally. Is the final value of s a or b? Below are the results of the execution
try..... 17 b 8 a
We found that the final result is a, but b is printed out with priority over a. Why is this? Through debug step-by-step debugging, we found that before executing return in try, the code in finally will be executed, and then the return statement will be executed. If the return statement is finally included, what will happen? Let's look at the second situation.
The second case: try and finally contain return statements. We slightly change the above code.
public class TestTry { static String s=""; public static void main(String args[]){ s = test1(); System.out.println("8 "+s); } public static String test1(){ try { System.out.println("try......"); return s = "a"; } finally{ return s="b"; } } }What kind of results will occur if s="b"; in finally be changed to return s="b";? Is the string s a or b?
try..... 8 b
We found that the final print result is b.
We know that the return statement is used in a certain method. One is used to return the execution result of the function, and the other is used in functions with a return value of void type. It is just a return statement (return ;), which is used to end the method at this time. The execution of , that is, the statement after this return will not be executed. Of course, in this case, there can be no other statement after the return statement.
public static int print() { int c = 1; try { c++; System.out.println("try execution..."); return c+100; //---------1 } catch (Exception e) { e.printStackTrace(); //return c; //----------4 } finally { c++; System.out.println("finally executing..."); return c ; //---------2 } //return c; //--------------3 }The result of program execution is:
run:
Try execution...
Finally executing...
3
Successfully built (total time: 0 seconds)
Note that there can only be one return statement at positions 2 and 3, and there must be one return statement in 2, 3, and 4. If 2 is executed, the method will end and the statement at 3 cannot be executed.
When the try statement block is executed at 1, the return value of the function will be stored in another temporary variable (different variables from c, its value is 102). Since no exception occurs, the finally statement will be executed immediately. Block, another statement block is encountered at 2, and the return value is stored in the temporary variable (value is 3). The final return value is the value of the temporary variable here. After the return execution is completed, the method ends.
public static int print() { int c = 1; try { c++; System.out.println(c); System.out.println("try"); return c+100; //------- -1 } finally { c++; System.out.println(c); System.out.println("finally"); } }In the above code, there is no return statement in the finally statement block. The function finally returns the value of the first temporary variable, that is, 102. The execution result is as follows:
run:tryfinally102 successfully built (total time: 0 seconds)
Conclusion: 1. Regardless of whether there is a return statement in the try, the finally statement will definitely be executed (it is finally, and the name is good).
2. If there is no return statement in finally and there is a return in try, then before executing the return statement in try, the code in finally will be executed first, and then the return statement in try; if the return statement is also included in finally, It will return directly and no longer execute the return statement in the try.