Java basic interview question: there is a return statement in try {}, so will the code in finally {} following this try be executed? When will the code be executed, before or after return ?,

Source: Internet
Author: User

Java basic interview question: there is a return statement in try {}, so will the code in finally {} following this try be executed? When will the code be executed, before or after return ?,

Package com. swift; public class Try_Catch_Finally_Test {public static void main (String [] args) {/** try {} contains a return statement, so will the code in finally {} following this try be executed, when will it be executed, before or after return? */System. out. println ("execute return without returning the value first, save the value for temporary storage, wait until finally execution is complete and then return the saved value" + new Try_Catch_Finally_Test (). test ();} static int test () {int x = 1; try {return ++ x;} finally {++ x; System. out. println ("finally first executed, return last executed" + x );}}}

Return also exists in finally, and finally return in finally

Package com. swift; public class Try_Catch_Finally_Test {public static void main (String [] args) {/** try {} contains a return statement, so will the code in finally {} following this try be executed, when will it be executed, before or after return? */System. out. println ("execute return without returning the value first, save the value for temporary storage, wait until finally execution is complete and then return the saved value" + new Try_Catch_Finally_Test (). test ();} static int test () {int x = 1; try {return ++ x;} catch (Exception e) {e. printStackTrace (); return ++ x;} finally {++ x; System. out. println ("finally first executed, return last executed" + x); return ++ x ;}}}

Some people say that the return statement is executed twice and finally is executed in the middle. This is understandable and may be easy to memorize.

But I think return is executed after finally. After finally is executed, return the returned value to the call. Note that the returned value is not the final value, instead, return will return the returned value, but the value temporarily saved because it cannot be returned in finally. There is an interruption here.

 

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.