1、finally是否执行:ide
1.只有与 finally 相对应的 try 语句块获得执行的状况下,finally 语句块才会执行
当finally 相对应的 try 语句块以前,已经抛出错误,或者已经返回,return,就不会执行finallyspa
2.当与 finally 相对应的 try 语句块中有 System.exit(int status) ,而且顺利执行,finally 语句块也不会执行
System.exit(int status) 会终止 Java 虚拟机的运行
相似的状况还有:线程被中断,interrupted,进程被终止,killed,死机、断电等线程
2、finally什么时候执行:code
1.Java 虚拟机会把 finally 语句块做为 subroutine 直接插入到 try 语句块或者 catch 语句块的控制转移语句以前。
控制转移语句:return、throw 和 break、continueblog
2.对于有返回值的 return 和 throw 语句,在执行 subroutine 以前,try 或者 catch 语句块会保留其返回值到本地变量表(Local Variable Table)中。
待 subroutine 执行完毕以后,再恢复保留的返回值到操做数栈中,而后经过 return 或者 throw 语句将其返回给该方法的调用者(invoker)。进程
1 public class Test { 2 3 public static void main(String[] args) { 4 System.out.println(testFinally()); 5 } 6 7 public static int testFinally() { 8 int i = 1; 9 // if(i == 1) 10 // return 0; 11 System.out.println("before try"); 12 i = i / 0; 13 try { 14 System.out.println("try"); 15 //System.exit(0); 16 return i; 17 } finally { 18 System.out.println("finally"); 19 } 20 } 21 }
1 public class Test { 2 3 public static void main(String[] args) { 4 System.out.print(testFinally()); 5 } 6 7 public static int testFinally() { 8 int b = 10; 9 try { 10 System.out.println("try"); 11 return b += 20; 12 } catch (Exception e) { 13 System.out.println("error:" + e); 14 } finally { 15 System.out.println("finally"); 16 System.out.println("b:" + b); 17 b = 50; 18 System.out.println("b:" + b); 19 } 20 return 40; 21 } 22 }