Execution order of finally and return statements in Java

Source: Internet
Author: User
Tags finally block

Is the Java finally statement executed before or after return?

There are a lot of people on the Internet to explore the mechanism of exception capture in Java is the finally statement in the Try...catch...finally block bound to be executed? A lot of people say no, of course their answer is correct, after I test, at least in two cases the finally statement will not be executed:

(1) The Try statement is not executed, such as when it is returned before the try statement, so that the finally statement is not executed, which also illustrates the need for a finally statement to be executed rather than a sufficient condition: the corresponding Try statement must be executed.

(2) There is system.exit (0) in the try block, such statements, System.exit (0), terminating the Java Virtual Machine JVM, even the JVM stops, and all ends, and of course the finally statement is not executed.

Of course, there are many people who explore the relationship between the execution of finally statement and return, rather confusing, do not know finally statement is executed before or after a try? I am also a confused, I think they are not correct, I think it should be: Finally the statement is executed after the return statement of the try is executed, and before returning returns. This is a bit contradictory, perhaps I am not very clear statement, the following I gave some of the results of their own experiments and examples to prove that there are any questions to welcome you.

1. Finally statement is executed after Return statement is executed.

public class FinallyTest1 {public  
      
    static void Main (string[] args) {  
              
        System.out.println (test1 ());  
    }  
      
    public static int test1 () {  
        int b =;  
      
        try {  
            System.out.println ("try block");  
      
            return B + +;   
        }  
        catch (Exception e) {  
      
            System.out.println ("Catch block");  
        }  
        finally {  
                  
            System.out.println ("finally Block");  
                  
            if (b >) {  
                System.out.println ("b>25, B =" + B);  
            }  
              
        return b;  
    }  
          
}

The results of the operation are:

Try block  
finally block  
b>25, b =
100

Description The return statement has been executed to execute the finally statement, but it does not return directly, but waits until the finally statement is executed and returns the result.

If the case is not considered sufficient to illustrate this situation, add an example to reinforce the conclusion that:

public class FinallyTest1 {public  
      
    static void Main (string[] args) {  
              
        System.out.println (test11 ());  
    }  
          
    public static String test11 () {  
        try {  
            System.out.println (' try Block ');  
      
           return test12 ();  
      } finally {  
           System.out.println ("finally Block");  
       }  
      
  public static String test12 () {  
       System.out.println (' return statement ');  
      
       Return ' after return ';  
   }  
          

The results of the operation are:

Try block return  
statement  
finally blocks after return  

Describes that the return statement in try executes first but not immediately, until finally execution is completed and then

Here you may think: If finally there is a return statement, then is not directly returned, try in the return can not be returned? Look below.

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.