关于finally代码块是否一定被执行的问题

一般来说,只要执行了try语句,finally就会执行

但是,有以下几种情况需要特殊考虑

具体例子看链接  点击这里

第一点

try代码块没有被执行,意思就是错误在try代码块之前就发生了。

第二点

 1 public class SystemExitAndFinally {
 2 
 3     
 4     public static void main(String[] args)
 5     {
 6         
 7         try{
 8 
 9             
10             System.out.println("in main");
11             
12             throw new Exception("Exception is thrown in main");
13 
14                     //System.exit(0);
15 
16         
17         }
18         
19         catch(Exception e)
20 
21             {
22             
23             System.out.println(e.getMessage());
24             
25             System.exit(0);
26         
27         }
28         
29         finally
30         
31         {
32             
33             System.out.println("in finally");
34         
35         }
36     
37     }
38 
39 
40 }

出现了System.exit(0);          一旦出现这个,会退出当前java虚拟机,程序就停止了。

原文地址:https://www.cnblogs.com/xcl666/p/9938956.html