java throw Anomaly and the finally mixed to the order of execution of a block of statements

code show as below:

 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

 

 FIG operation results as:

For the analysis of the code: the first three lines of output, try the three is the normal output statements, and finally throws a by zero error. Then catch phrase to capture this statement, and output statements. But we will find the back of the catch statements have not been implemented, but directly executed finally statement, the statement explained finally changed the logic of the output code. But the specific cause is not clear.

 Finally, a small question: finally statement will execute it?

I may answer this question yourself, if you use the exit before finally, quit the program, then finally statement will not be executed.

For example, we use the following test code:

 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

Run results shown in Figure:

We can see that, after finally statement has not been output, the statement before exiting the program does not have any influence.

Guess you like

Origin www.cnblogs.com/wushenjiang/p/11762084.html
Recommended