Reprint: In case of return, try Catch finally execution order (most persuasive summary)

Source: Internet
Author: User
Tags finally block try catch

Turn from: http://blog.csdn.net/kavensu/article/details/8067850

Directory:

Conclusion Example Final conclusion

ConclusionsThe code executes in the finally block, regardless of the occurrence of the wood, and finally still executes when there is return in the try and catch, and finally is executed after the expression operation after returning (at which point no value is returned after the operation). Instead of saving the value to be returned, the code in the Finally, the return value will not change, still is the previously saved value, so the function return value is determined before finally execution; Finally, it is best not to include return, otherwise the program will quit early, The return value is not a return value saved in a try or catch.examplesCondition 1:try{} catch () {}finally{} return;
Obviously the program executes sequentially. Condition 2:try{return;} catch () {} finally{} return;
The program executes the code in the try block before return (including the expression operation in the return statement);
Then execute the finally block, and finally execute the try return;
The statement return after the finally block, because the program has been return in a try and is no longer executing. Condition 3:try{} catch () {return;} finally{} return;
The program executes a try first, and if an exception is encountered to execute a catch block,
Exception: Executes the code in the catch before return (including the expression operation in the return statement), and executes all the code in the finally statement.
The last execution of the catch block is return. Finally, the code at 4 is no longer executing.
No exception: Execute try and finally return. Condition 4:try{return;} catch () {} Finally{return;}
The program executes the code in the try block before return (including the expression operation in the return statement);
The finally block is executed again because there is a return in the finally block and exits early. Condition 5:try{} catch () {return;} Finally{return;}
The program executes the code in the Catch block before return, including an expression operation in the return statement;
The finally block is executed again because there is a return in the finally block and exits early. Condition 6:try{return;} catch () {return;} Finally{return;}
The program executes the code in the try block before return (including the expression operation in the return statement);
Exception: Executes the code in the Catch block before return (including the expression operation in the return statement);
The finally block is executed again because there is a return in the finally block and exits prematurely.
No exception: Then the finally block is executed because there is a return in the finally block and exits early.Final Conclusion

A finally statement is executed before any return statement in a try or catch is executed, if finally exists.

If finally there is a return statement, then the program will return, so finally in the return is bound to be return,

The compiler implements the finally return to a warning.

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.