About the execution sequence of finally and return (to be supplemented), finallyreturn

Source: Internet
Author: User

About the execution sequence of finally and return (to be supplemented), finallyreturn

Code 1:

public abstract class Test {    public static void main(String[] args) {        System.out.println(beforeFinally());    }         public static int beforeFinally(){        int a = 0;        try{            a = 1;            return a;        }finally{            a = 2;        }    }}/**output:1*/

From the result, it seems that the statement in 'Finally 'is executed after 'Return'. Actually, the statement in 'Finally 'is actually executed before 'Return. So the question is, since it was executed before, why is the value of 'A' not overwritten?
The actual process is as follows: when the program executes the return method in the try {} statement, it will do this and store the returned results in a temporary stack, then the program will not return immediately, but will execute the program in finally {}. When executing 'a = 2', the program will only overwrite the value of, but it does not update the value to be returned in the temporary stack. After the execution, the main program will be notified that "the finally program has been executed and can be requested to return". At this time, the value in the temporary stack will be retrieved and returned. It should be clear that the value to be returned is saved to the temporary stack.

Zookeeper ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code 2:

public abstract class Test {    public static void main(String[] args) {        System.out.println(beforeFinally());    }         public static int beforeFinally(){        int a = 0;        try{            a = 1;            return a;        }finally{            a = 2;            return a;        }    }}/**output:2*/

Here, there is also a return in finally {}, so when this return is executed, the value in the temporary stack will be updated. Similarly, after finally execution, the main program will be notified to return the request, which is about to return the value in the temporary stack. Therefore, the returned value is 2.

Zookeeper ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Code 3:

Public class Test {public int add (int a, int B) {try {return a + B;} catch (Exception e) {System. out. println ("catch statement block");} finally {System. out. println ("finally statement block");} return 0;} public static void main (String argv []) {Test test = new Test (); System. out. println ("And yes:" + test. add (9, 34) ;}}/** output: finally statement block and yes: 43 */

Zookeeper ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Summary:

1. The finally {} code block is executed before return. 2. Multiple return statements are executed in sequence. After multiple return statements are executed, the subsequent return statements will not be executed. 3. Remember that, no matter whether an exception is thrown, finally will execute before 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.