1. 程式人生 > >動手動腦(五)

動手動腦(五)

src try class visio 復制 out win pan dialog

一,

 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 }

程序運行截圖:

技術分享圖片

 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 int i=1, j=0, k; 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 }

程序運行截圖:

技術分享圖片

技術分享圖片

程序分析:

try語句中運行發生異常,跳轉到catch語句中執行System.out.println("被0除. "+ e.getMessage());語句,所以輸出如上。無論是否發生異常最後執行了finally語句,彈出上述頁面防止程序崩潰。

二,

 1 public class CatchWho { 
 2     public static void main(String[] args) { 
 3         try { 
 4                 try { 
 5                     throw new ArrayIndexOutOfBoundsException(); 
 6                 } 
 7                 catch(ArrayIndexOutOfBoundsException e) { 
 8                        System.out.println(  "ArrayIndexOutOfBoundsException" +  "/內層try-catch"); 
 9                 }
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 }

運行程序截圖:

技術分享圖片

運行後思考:

我們根據程序運行的結果可以知道程序實際上式執行了第8行與第14行的輸出語句,而沒有執行第17行。

 1 public class CatchWho2 { 
 2     public static void main(String[] args) { 
 3         try {
 4                 try { 
 5                     throw new ArrayIndexOutOfBoundsException(); 
 6                 } 
 7                 catch(ArithmeticException e) { 
 8                     System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); 
 9                 }
10             throw new ArithmeticException(); 
11         } 
12         catch(ArithmeticException e) { 
13             System.out.println("發生ArithmeticException"); 
14         } 
15         catch(ArrayIndexOutOfBoundsException e) { 
16             System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); 
17         } 
18     } 
19 }

運行截圖:

技術分享圖片

運行後截圖:

我們根據程序運行的結果可以知道程序實際上式執行了第16行的輸出語句,而沒有執行第8行與第13行。

總結分析:

通過上述兩個實例,我們可以總結出java多層嵌套異常處理的基本流程:

try語句可以被嵌套。也就是說,一個try語句可以在另一個try塊內部。每次進入try語句,異常的前後關系都會被推入堆棧。如果一個內部的try語句不含特殊異常的catch處理程序,堆棧將彈出,下一個try語句的catch處理程序將檢查是否與之匹配。這個過程將繼續直到一個catch語句匹配成功,或者是直到所有的嵌套try語句被檢查耗盡。如果沒有catch語句匹配,Java的運行時系統將處理這個異常。

三,

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

運行截圖:

技術分享圖片

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

四,

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

運行截圖:

技術分享圖片

實驗總結:

如果finally塊中的代碼過多會導致字節碼條數”膨脹”,因為finally中的字節碼會被”復制”到try塊和所有的catch塊中,出異常了。System.out.println("F")不在finally裏。而只有finally裏的語句才會不論如何都會執行。

動手動腦(五)