1. 程式人生 > >java之異常處理

java之異常處理

init value etc .com level dem com ini war

AboutException的測試:

 1 package test;
 2 import javax.swing.*;
 3 
 4 class AboutException {
 5    public static void main(String[] a) 
 6    {
 7       @SuppressWarnings("unused")
 8     int i=1, j=0, k;
 9       //System.out.println("第一次直接對兩個整數進行除法運算結果:");
10       //k=i/j;
11 
12 
13     try
14     {
15         System.out.println("第五次測試finally語句在沒有報錯的前提下以及提前退出語句下是否會被執行:");
16 //System.out.println("第二次將兩個整數放在try{}catch{}函數中進行除法運算結果:"); 17 k = i/j; // Causes division-by-zero exception 18 //throw new Exception("Hello.Exception!"); 19 //System.out.println("第三次測試finally語句在沒有報錯的情況下是否會被執行:"); 20 21 } 22 23 24 25 catch (Exception e)
26 { 27 28 System.out.println(e.getMessage()); 29 System.exit(0); 30 31 } 32 33 34 finally 35 { 36 JOptionPane.showConfirmDialog(null,"OK"); 37 } 38 39 } 40 }

1.第一次

技術分享圖片

技術分享圖片

因為0不能作為分母而報錯。

第二次

技術分享圖片

可以看到在try{}catch{}函數中雖然沒有報錯,但運算結果是將報錯信息輸出:/ by zero,

可以看出try{} catch{}函數的作用,還可以看到錯誤的信息。

第三次

技術分享圖片

技術分享圖片

可以看到,在程序沒有報錯的情況下,finally任然會被執行。

第四次

技術分享圖片

技術分享圖片

可以看到,在報錯的前提下,加入提前退出語句後只輸出了錯誤信息,而沒有執行finally語句。

第五次

技術分享圖片

可以看到,在沒有報錯的前提下,catch語句不能正常執行,所以提前退出語句不執行,finally語句得以正常執行。

綜上所述,有

(1)Finally語句不管有沒有異常發生,finally語句都得以運行,但是在遇到提前退出語句後不能被執行。

(2)try{}catch{}函數能將錯誤信息拋出.

ThrowDemo的測試:

 1 package test;
 2 public class ThrowDemo { 
 3     public static void main(String[] args) { 
 4 //        try {
 5             double data = 100 / 0.0;
 6             System.out.println("浮點數除以零:" + data); 
 7 //            if(String.valueOf(data).equals("Infinity"))
 8 //            { 
 9 //                System.out.println("In Here" ); 
10 //                throw new ArithmeticException("除零異常");
11 //            }
12 //        } 
13 //        catch(ArithmeticException e) { 
14 //            System.out.println(e); 
15 //        } 
16     } 
17 }
18  

1.第一次測試

技術分享圖片

沒有報錯,但是如果將0.0改為0就會有如下結果:

技術分享圖片

這個錯誤相比不是很陌生了,因為在上一個測試中我們遇到了無數次這個錯誤類型,就是因為分母為零,根據Java對double的定義運算,結果為無限。

2.第二次測試

技術分享圖片

系統拋出新的錯誤被截取,

3.第三次測試

  技術分享圖片

錯誤被截取後正常輸出。技術分享圖片

在這個測試裏,第二個catch沒有被編譯執行。

CatchWho測試:

 1 package test;
 2 public class CatchWho { 
 3     public static void main(String[] args) { 
 4         try { //2
 5                 try { //1
 6                     throw new ArrayIndexOutOfBoundsException(); 
 7                 } 
 8                 catch(ArrayIndexOutOfBoundsException e) { 
 9                        System.out.println(  "ArrayIndexOutOfBoundsException" +  "/內層try-catch"); 
10                 }
11  
12             throw new ArithmeticException(); //扔出新的錯誤
13         } 
14         catch(ArithmeticException e) { 
15             System.out.println("發生ArithmeticException"); 
16         } 
17         catch(ArrayIndexOutOfBoundsException e) { //沒有輸出,因為前面有一個catch將錯誤捕捉,這個就不會被執行了
18            System.out.println(  "ArrayIndexOutOfBoundsException" + "/外層try-catch"); 
19         } 
20     } 
21 }

測試結果

技術分享圖片

拋出錯誤類型。

CatchWho2測試:

 1 package test;
 2 public class CatchWho2 { 
 3     public static void main(String[] args) { 
 4         try {
 5                 try { //第一次扔出錯誤
 6                     throw new ArrayIndexOutOfBoundsException(); 
 7                 } 
 8                 catch(ArithmeticException e) {//截取錯誤,但沒有輸出
 9                     System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); 
10                 }
11             throw new ArithmeticException(); //第二次扔出錯誤
12         } 
13         catch(ArithmeticException e) { //不是正確的錯誤類型,錯誤類型不匹配,不輸出
14             System.out.println("發生ArithmeticException"); 
15         } 
16         catch(ArrayIndexOutOfBoundsException e) { //截取錯誤類型
17             System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); 
18         } 
19     } 
20 }

EmbededFinally測試;

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

測試結果:

技術分享圖片

總結:

當有多層嵌套的finally時,異常在不同的層次拋出,在不同的位置拋出,可能會導致不同的finally語句塊執行順序。

java之異常處理