1. 程式人生 > 實用技巧 >動手動腦之異常處理

動手動腦之異常處理

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不會執行。