动手动脑之异常处理

1.实例

 1 import javax.swing.*;
 2 
 3 class AboutException {
 4    public static void main(String[] a) 
 5    {
 6       int i=1, j=0, k;
 7       k=i/j;
 8 
 9 
10     try
11     {
12         
13         k = i/j;    // Causes division-by-zero exception
14         //throw new Exception("Hello.Exception!");
15     }
16     
17     catch ( ArithmeticException e)
18     {
19         System.out.println("被0除.  "+ e.getMessage());
20     }
21     
22     catch (Exception e)
23     {
24         if (e instanceof ArithmeticException)
25             System.out.println("被0除");
26         else
27         {  
28             System.out.println(e.getMessage());
29             
30         }
31     }
32 
33     
34     finally
35          {
36              JOptionPane.showConfirmDialog(null,"OK");
37          }
38         
39   }
40 }

 

2.基础知识

(1)Java中的异常捕获语句:

  • Try{

  •      //可能发生运行错误的代码;

  • }

  • catch(异常类型 异常对象引用){

  •      //用于处理异常的代码

  • }

  • finally{

  •      //用于“善后” 的代码

  • }

(2)使用Java异常处理机制

  • 把可能会发生错误的代码放进try语句块中。

  • 当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。

  • 当异常发生时,程序控制流程由try语句块跳转到catch语句块。

  • 不管是否有异常发生,finally语句块中的语句始终保证被执行。

  • 如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。


3.多层的异常捕获

 1 public class CatchWho 
 2 { 
 3     public static void main(String[] args) 
 4     { 
 5         try 
 6     { 
 7                 try 
 8         { 
 9                     throw new ArrayIndexOutOfBoundsException(); 
10                 } 
11                 catch(ArrayIndexOutOfBoundsException e) 
12         { 
13                        System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); 
14                 }
15  
16             throw new ArithmeticException(); 
17         } 
18         catch(ArithmeticException e) 
19     { 
20             System.out.println("发生ArithmeticException"); 
21         } 
22         catch(ArrayIndexOutOfBoundsException e) 
23     { 
24            System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
25         } 
26     } 
27 }

 1 public class CatchWho2 
 2 { 
 3     public static void main(String[] args) 
 4     { 
 5         try 
 6     {
 7                 try 
 8         { 
 9                     throw new ArrayIndexOutOfBoundsException(); 
10                 } 
11                 catch(ArithmeticException e) 
12         { 
13                     System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
14                 }
15             throw new ArithmeticException(); 
16         } 
17         catch(ArithmeticException e) {
18      
19             System.out.println("发生ArithmeticException"); 
20         } 
21         catch(ArrayIndexOutOfBoundsException e) 
22     { 
23             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
24         } 
25     } 
26 }

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

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

结论:Java通过使用try…catch…finally语句来捕获一个或多个异常,catch语句可以有一个或多个,而且至少要一个catch语句或finally。当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。如果某处发生异常,则try语句中此处之后的代码都不会被执行。


4.实例

 1 public class SystemExitAndFinally 
 2 {
 3 
 4     
 5     public static void main(String[] args)
 6     
 7     {
 8         
 9         try
10         {
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 }

结论:

  1. finally相对应的try语句得到执行的情况下,finally才有可能执行。

  2. finally执行前,程序或线程终止,finally不会执行。


原文地址:https://www.cnblogs.com/znjy/p/13987070.html