Execution of a return statement in a try Catch finally statement in Java

Source: Internet
Author: User
Tags finally block try catch

The contents of the finally block are executed before the return statement in the try, and if there is a return statement in the Finall statement block, it is returned directly from Finally, which is also the reason why the return in finally is not recommended. Here are a few things to look at.

Situation one (there is no return in return,finally in try):

public class trytest{public      static void Main (string[] args) {          System.out.println (test ());      }        private static int Test () {          int num = ten;          try{              System.out.println ("try");              return num + = +;          } catch (Exception e) {              System.out.println ("error");          } finally{              if (num >) {                  System.out.println ("num>20:" + num);              }              System.out.println ("finally");          }          return num;      }  }  

The output results are as follows:

Try
Num>20:90
Finally
90

Analysis: apparently "return num + = 80" was split into "num = num+80" and "return num" two statements, the line executes the "num = num+80" statement in the try, saves it, and before "return num" executes in try, The statement in finally is executed before the 90 is returned.

Case two (both try and finally have return):

public class trytest{public      static void Main (string[] args) {          System.out.println (test ());      }        private static int Test () {          int num = ten;          try{              System.out.println ("try");              return num + = +;          } catch (Exception e) {              System.out.println ("error");          } finally{              if (num >) {                  System.out.println ("num>20:" + num);              }              System.out.println ("finally");              num = +;              return num;}}  }  

  

The output results are as follows:

Try
Num>20:90
Finally
100

Analysis: The return statement in a try is also split, and finally the return statement executes before the return statement in the try, so that return in the try is "overwritten" and is no longer executed.

Case three (Finally, change the return value num):

public class trytest{public      static void Main (string[] args) {          System.out.println (test ());      }        private static int Test () {          int num = ten;          try{              System.out.println ("try");              return num;          } catch (Exception e) {              System.out.println ("error");          } finally{              if (num >) {                  System.out.println ("num>20:" + num);              }              System.out.println ("finally");              num = +;          }          return num;      }  }  

The output results are as follows:

Try

Finally

10

Analysis: Although the return value num is changed in Finally, the test () function obtains the value of NUM returned in the try after executing the statement in Finally, because there is no return value for the NUM in finally. The value of num in try is still the value left before the program enters the finally code block, so the resulting return value is 10.

But let's look at the following (wrapping the value of num in the NUM Class):

public class trytest{public      static void Main (string[] args) {          System.out.println (Test (). num);      }        private static num Test () {          num number = new num ();          try{              System.out.println ("try");              return number;          } catch (Exception e) {              System.out.println ("error");          } finally{              if (Number.num >) {                  System.out.println ("number.num>20:" + number.num);              }              System.out.println ("finally");              Number.num = +;          }          return number;      }  }    Class num{public      int Num = ten;  }  

The output results are as follows:

Try
Finally
100

As can be seen from the results, the return value of NUM is changed in Finally, in case three, not returned by the return in the try (The test () method does not get 100), but here is returned by the return statement in the try.

To analyze the above situation, we need to drill down into the operation of the Operation Stack when the program executes the bytecode instruction in exection_table in the JVM virtual machine, can refer to http://www.2cto.com/kf/201010/76754.html In this article, you can also refer to the "Deep Java Virtual machine: JVM advanced features and Best practices" section in chapter 6th on the collection of attribute tables.

For cases with return statements, here we can briefly summarize the following:

The Try statement finishes all other operations before returning, retains the value to return, and then goes to the statement in the execute Finally, which is then divided into the following three cases:

Case one: If there is a return statement in Finally, the return statement in the try is "overwritten", executes the return statement directly in the Finally, and returns the value so that the returned value cannot be preserved until the try is obtained.

Case two: If there is no return statement in finally and no change to return the value, then after executing the statement in Finally, the return statement in the try is executed, returning the previously reserved value.

Scenario Three: If there is no return statement in Finally, but changes the value to return, here is a bit like the difference between reference passing and value passing, in the following two cases:

1) If the return data is a base data type or a text string, then the change to the base data in finally does not work, and the return statement in the try will still return the value reserved before entering the finally block.

2) If the return data is a reference data type, and in finally the change to the property value of the reference data type is in effect, the return statement in the try returns the value of the property after the change in finally.

Reprint Source: http://blog.csdn.net/ns_code/article/details/17485221

Execution of a return statement in a try Catch finally statement in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.