Is Java's finally statement executed before or after return?

There are many people on the Internet discussing the exception capture mechanism in Java. Will the finally statement in the try...catch...finally block be executed? Many people say no, of course their answers are correct. After my experiments, there are at least two cases where the finally statement will not be executed:

(1) The try statement is not executed. For example, it returns before the try statement, so the finally statement will not be executed. This also shows that the necessary but not sufficient condition for the finally statement to be executed is that the corresponding try statement must be executed. arrive.

(2) There is a statement like System.exit(0); in the try block, System.exit(0); is to terminate the Java virtual machine JVM, even the JVM is stopped, everything is over, of course, the finally statement does not will be executed.

The finally statement is executed after the return statement of the try is executed and before the return returns. public class FinallyTest1 {
public static void main(String[] args) {
System.out.println(test1());
}

public static int test1() {
int b = 20;
try {
System.out.println("try block ");
return b += 80;
}
catch (Exception e) {
System.out.println("catch block");
}
finally {
System.out.println("finally block");
if (b > 25) {
System.out.println("b>25, b = " + b);
}
}
return b;
}

}

The result of running is:

try block
finally block
b>25, b = 100
100

It means that the return statement has been executed and then the finally statement is executed, but it does not return directly, but returns the result after the finally statement is executed.

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";
   }   
}

It means that the return statement in try is executed first but does not return immediately, and it will wait until finally execution ends.

Here you may think: if there is also a return statement in finally, will it return directly, and the return in try cannot be returned? look down.

2. finally块中的return语句会覆盖try块中的return返回。

public class FinallyTest2 {
    public static void main(String[] args) {
        System.out.println(test2());
    }
    public static int test2() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            return 200;
        }
        // return b;
    }
}
运行结果是:

try block
finally block
b>25, b = 100
200
这说明finally里的return直接返回了,就不管try中是否还有返回语句,这里还有个小细节需要注意,finally里加上return过后,finally外面的return b就变成不可到达语句了,也就是永远不能被执行到,所以需要注释掉否则编译器报错。


3. 如果finally语句中没有return语句覆盖返回值,那么原来的返回值可能因为finally里的修改而改变也可能不变。

public class FinallyTest3 {
    public static void main(String[] args) {
        System.out.println(test3());
    }


    public static int test3() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            b = 150;
        }
        return 2000;
    }
}


The result of running is:

try block
finally block
b>25, b = 100
100

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735950&siteId=291194637