Java Basic interview question: try{} has a return statement, then the last {} immediately after the try code will be executed, when executed, before or after the return?

Source: Internet
Author: User

 PackageCom.swift; Public classTry_catch_finally_test { Public Static voidMain (string[] args) {/** There is a return statement in try{}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return? */System.out.println ("Execute return does not return a value, the value is staged, waiting for finally to finish executing and then return the saved value" +Newtry_catch_finally_test (). Test ()); }    Static intTest () {intx = 1; Try {            return++x; } finally {            ++x; System.out.println ("Finally executes first, return finally executes" +x); }    }}

Finally there is a return, and finally returns in the finally

 PackageCom.swift; Public classTry_catch_finally_test { Public Static voidMain (string[] args) {/** There is a return statement in try{}, then the code in the finally {} immediately after this try will not be executed, when executed, before or after the return? */System.out.println ("Execute return does not return a value, the value is staged, waiting for finally to finish executing and then return the saved value" +Newtry_catch_finally_test (). Test ()); }    Static intTest () {intx = 1; Try {            return++x; }Catch(Exception e) {e.printstacktrace (); return++x; }finally {            ++x; System.out.println ("Finally executes first, return finally executes" +x); return++x; }    }}

Some say that return executes 2 times and finally executes in the middle, which can be understood and may also be convenient rote

But I think that return is executed after finally, after finally execution returns the return value to the call, only to notice that the returned value is not the final value, but that return will return the return value but is temporarily saved because finally appears unable to return the value. Here's a break.

Java Basic interview question: try{} has a return statement, then the last {} immediately after the try code will be executed, when executed, before or after the return?

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.