动手动脑(五 异常处理)

 1 package class5;
 2 
 3 import javax.swing.JOptionPane;
 4 
 5 class AboutException {
 6        public static void main(String[] a) 
 7        {
 8           int  i=1, j=0, k;
 9           k=i/j;
10 
11 
12         try
13         {
14             
15             k = i/j;    // Causes division-by-zero exception
16             //throw new Exception("Hello.Exception!");
17         }
18         
19         catch ( ArithmeticException e)
20         {
21             System.out.println("被0除.  "+ e.getMessage());
22         }
23         
24         catch (Exception e)
25         {
26             if (e instanceof ArithmeticException)
27                 System.out.println("被0除");
28             else
29             {  
30                 System.out.println(e.getMessage());
31                 
32             }
33         }
34 
35         
36         finally
37          {
38                  JOptionPane.showConfirmDialog(null,"OK");
39          }
40             
41       }
42     }
1.AboutException:
一般情况下在没有定义提供相应的异常处理代码,JVM将会结束掉整个应用程序。当自己定义异常异常处理方法时,把可能出现错误的代码放在try语句中,当执行该代码时会时时的进行监控,只要代码
中出现错误,就会抛出一个异常对象,异常处理代码就会捕获并且处理这个错误,错误的处理代码将放在catch语句中,当异常发生时,程序控制流程由try语句块跳转到catch语句块,不管是否有异常
发生,finally语句块中的语句始终保证被执行。



2.CatchWho.java:

运行结果:

3.CatchWho2.java:

运行结果:

4.EmbedFinally.java:

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

 运行结果

总结:当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序

5.finally语句块一定会执行吗?

SystemExitAndFinally.java

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

运行结果:

由结果可知,在此程序中并没有执行finally语句,因为程序中使用了system.exit语句,使得程序在执行finally语句之前就终止了。

6.

之所以会出现这种不同的情况,是因为在处理int除以0时javac生成了idiv字节码指令,而double类型生成了ddiv字节码指令。

JVM在具体实现这两个指 令时,采用了不同的处理 策略,导致两段代码运行 时得到不同的结果。



猜你喜欢

转载自www.cnblogs.com/1gaoyu/p/9940145.html