Java exception handling has problems executing both the finally and return statements

  • 2020-04-01 04:24:25
  • OfStack

In Java, when the try and finally statements contain the return statements, the execution situation is exactly what, and whether the code in finally is executed or not, there is a variety of opinions, some say it will be executed, some say it will not be executed, which statement is correct, now through the following example to illustrate:
        The first case: a return statement is included in the try and not in the finally


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 the try and return it directly, and assign "b" to "s" in the finally, is the final value of s a or b? Here are the results of the execution


try..... 
17 b 
8 a 

  We found that the end result was a, but b came out ahead of a. Why? With debug, we found that the code in finally is executed before the return in the try, and then the return statement is executed. What happens if a return statement is also included in finally? Let's look at the second case.
        The second case: try, finally contains a return statement
          Let's change the above code a little bit


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";   
       
    } 
  } 
} 

  S in finally ="b"; I'll just say return s="b"; What's going to happen? Is the string s a or b?


try..... 
8 b 

  We found that the final print was b.
 

We know that the return statement is used in a certain method, one is used to return the result of the execution of the function, and the other is used to return a function of type void, just a return statement (return;). , which is used to end the execution of the method, that is, the statement after the 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 The execution of ...");
      return c+100; //--------1
    } catch (Exception e) {
      e.printStackTrace();
      //return c;  //--------4
    } finally {
      c++;
      System.out.println("finally The execution of ...");
      return c; //--------2
    }
    //return c;  //---------3
  }

The result of program execution is:


run:
try The execution of ...
finally The execution of ...
3
Successful build ( The total time : 0 seconds )

Note that there can only be one return statement at 2 and 3, and there must be one return statement at 2, 3, and 4. If the method ends after 2 is executed, the statement at 3 cannot be executed.

  The try block execution to 1, the deposit will return value from a function to another temporary variables (unlike c variables, it has a value of 102), because the exception occurs, then will perform the finally block, two places and meet a block, and store the return value to a temporary variable (a value of 3), the final return value of the variable is the temporary here, here is the return after completion of execution, the method to an end.


 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, and the function finally returns the value of the first temporary variable, namely 102. The execution result is as follows:


run:
try
finally
102
 Successful build  ( The total time : 0  seconds )

Conclusion: 1. The finally statement must be executed regardless of whether there is a return statement in the try.
              2. If there is no return statement in the finally and there is a return in the try, the code in the finally will be executed before the return statement in the try is executed. If you also include a return statement in finally, you will simply return instead of executing the return statement in the try.


Related articles: