A brief talk on return and finally problem in Java _java

Source: Internet
Author: User

These two days to learn the abnormal mechanism, which try...catch...finally personally feel that is an important link to the previous sentence, encountered the return and finally this fun question, after testing, found that the logic of computer language is really subtle, The following is your own opinion, if there is a mistake, look at the predecessors pointing:

First look at the first piece of code

 public class return_finally{public
   static void Main (string[] args) {
     System.out.println (m_1 ());
   }
   public static int m_1 () {
     int i=10;
     try{
       System.out.println ("start");
      return i+=10;
     } catch (Exception e) {
       System.out.println ("error:" +e);
     } finally{
       if (i>10) {
         System.out.println (i);
       }
       System.out.println ("finally");
     return i;
   }
 }

The first piece of code output is as follows:
Start
20
Finally
20
Note that at this point the second return is outside of the finally statement, and according to the return and finally at the same time, we can understand that the first return is only a condition, his role is only to find a finally statement, actually just perform a i+=10 operation , and then go directly to the finally statement and return the result.
Let's look at the second piece of code:

 public class return_finally{public
   static void Main (string[] args) {
     System.out.println (m_1 ());
   }
   public static int m_1 () {
     int i=10;
     try{
       System.out.println ("start");
      return i+=10;
      
     } catch (Exception e) {
       System.out.println ("error:" +e);
     } finally{
       if (i>10) {
         System.out.println (i);
       }
       System.out.println ("finally");
       return}}
 

The second code differs from the first paragraph just by putting the last return in the finally statement, and we can guess the result of the output:

Start

20

Finally

50

The return performed at this time overwrites the 20 of the previous result of the operation, and returns the value of 50, which means that returning in the finally statement is executed, er, it should be said.

There is also a third code, we slowly enjoy:

public class return_finally{public
  static void Main (string[] args) {
    System.out.println (m_1 ());
  }
  public static int m_1 () {
    int i=10;
    try{
      System.out.println ("start");
      return i;
    } catch (Exception e) {
      System.out.println ("error:" +e);
    } finally{
      if (i>10) {
        System.out.println (i);
      }
      System.out.println ("finally");
        i=50; 
    }
    return i;
  }
}

At this point in the finally statement more i=50, then what is the result?

Start

Finally

10

This is the result, and there is no return statement in the finally statement, so the original returned value does not change.

So the three examples can be understood in this way:

Hit return in the Try statement, first store the value to a place, and then look for the finally statement, if there is a new algorithm in the statement, from that space to take out the value of the operation, finally, there is return in the "new value" cover that space "old value", and eventually returned If finally there is no return, the "old value" in that space is taken out directly and returned back.

The above is purely understanding, I hope we have a lot of guidance, thank you for your help!

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.