java 抛出异常与finally的混用对于语句块的执行顺序的影响

代码如下:

 1 package test1;
 2 
 3 public class EmbededFinally {
 4 
 5     
 6     public static void main(String args[]) {
 7         
 8         int result;
 9         
10         try {
11             
12             System.out.println("in Level 1");
13 
14            
15              try {
16                 
17                 System.out.println("in Level 2");
18   // result=100/0;  //Level 2
19                
20                  try {
21                    
22                      System.out.println("in Level 3");
23                       
24                      result=100/0;  //Level 3
25                 
26                 } 
27                 
28                 catch (Exception e) {
29                     
30                     System.out.println("Level 3:" + e.getClass().toString());
31                 
32                 }
33                 
34                 
35                 finally {
36                     
37                     System.out.println("In Level 3 finally");
38                 
39                 }
40                 
41                
42                 // result=100/0;  //Level 2
43 
44             
45                 }
46             
47             catch (Exception e) {
48                
49                  System.out.println("Level 2:" + e.getClass().toString());
50            
51              }
52              finally {
53                 
54                 System.out.println("In Level 2 finally");
55            
56              }
57              
58             // result = 100 / 0;  //level 1
59         
60         } 
61         
62         catch (Exception e) {
63             
64             System.out.println("Level 1:" + e.getClass().toString());
65         
66         }
67         
68         finally {
69            
70              System.out.println("In Level 1 finally");
71         
72         }
73     
74     }
75 
76 }
View Code

 运行结果如上图:

对于代码的分析: 前三行输出结果,是try中三个正常的输出语句,并且在最后抛出了一个by zero错误。然后用catch语句捕捉了这个语句,并且输出了语句。但我们会发现后面的catch语句都没有被执行,而是直接执行了finally语句,说明finally语句改变了代码中输出的逻辑。但具体的原因尚不明确。

 最后,提出一个小问题:finally语句一定会执行吗?

我姑且自己回答一下,如果在finally之前使用了exit,退出了程序,那么finally语句就不会执行了。

比如,我们用以下的测试代码:

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

运行结果如图:

扫描二维码关注公众号,回复: 7680401 查看本文章

我们可以看到,finally后的语句并没有被输出,而在退出程序前的语句并没有任何影响。

猜你喜欢

转载自www.cnblogs.com/wushenjiang/p/11762084.html