
是的,即使在方法中的 return 语句之后,finally 块也会被执行。
Java中无论是否发生异常,finally块都会执行。如果我们在finally块中显式调用System.exit()方法,那么只有它不会被执行。很少有情况不会执行finally,例如JVM崩溃、电源故障、软件崩溃等。除了这些情况外, finally 块 将始终被执行。
示例
public class FinallyBlockAfterReturnTest {
public static void main(String[] args) {
System.out.println(<strong>count()</strong>);
}
public static int count() {
try {
<strong> return 1;
</strong> } catch(Exception e) {
<strong> return 2;
</strong> } finally {
System.out.println("Finally block will execute even after a return statement in a method");
}
}
}输出
Finally block will always excute even after a return statement in a method 1











