Questions about return and finally in Java

Source: Internet
Author: User

These two days to learn the abnormal mechanism, which try...catch...finally personally think is an important link, linked to the previous study of the sentence, encountered a return and finally this fun problem, after testing, found that the logic of computer language is really delicate, The following is their own opinion, if there is wrong, hope that the predecessors pointed out:

First look at the first piece of code

1  Public classreturn_finally{2      Public Static voidMain (string[] args) {3 System.out.println (M_1 ());4     }5      Public Static intm_1 () {6         inti=10;7         Try{8System.out.println ("Start" );9            returni+=10;Ten}Catch(Exception e) { OneSystem.out.println ("Error:" +e); A}finally{ -             if(i>10){ - System.out.println (i); the             } -System.out.println ("Finally" ); -         } -         returni; +     } -}
The first section of the code outputs the following results:
Start
20
Finally
20
Notice that at this point the second return is outside of the finally statement, and according to the same rule as return and finally, we can understand that the first return is just a condition, his function is only to find the finally statement, actually just perform a i+=10 operation , then go directly to the finally statement and return the result.
Let's look at the second piece of code:
1  Public classreturn_finally{2      Public Static voidMain (string[] args) {3 System.out.println (M_1 ());4     }5      Public Static intm_1 () {6         inti=10;7         Try{8System.out.println ("Start" );9            returni+=10;Ten             One}Catch(Exception e) { ASystem.out.println ("Error:" +e); -}finally{ -             if(i>10){ the System.out.println (i); -             } -System.out.println ("Finally" ); -             return50;  +         } -     } +}

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

Start

20

Finally

50

The return executed at this point already covers 20 of the result of the previous operation, and returns the value of 50, which means that the return in the Finally statement is executed, and, well, it should be.

And the third piece of code, everyone slowly enjoy:

 Public classreturn_finally{ Public Static voidMain (string[] args) {System.out.println (m_1 ()); }     Public Static intm_1 () {inti=10; Try{System.out.println ("Start" ); returni; }Catch(Exception e) {System.out.println ("Error:" +e); }finally{            if(i>10) {System.out.println (i); } System.out.println ("Finally" ); I=50; }        returni; }}

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

Start

Finally

10

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

So, through these three examples, can this be understood:

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

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

    

Questions about return and finally 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.