1. 程式人生 > >Java課程作業之動手動腦(五)

Java課程作業之動手動腦(五)

1.請閱讀並執行AboutException.java示例。

import javax.swing.*;

class AboutException {
   public static void main(String[] a) 
   {
      int i=1, j=0, k;
      k=i/j;


    try
    {
        
        k = i/j;    // Causes division-by-zero exception
        //throw new Exception("Hello.Exception!");
    }
    
    
catch ( ArithmeticException e) { System.out.println("被0除. "+ e.getMessage()); } catch (Exception e) { if (e instanceof ArithmeticException) System.out.println("被0除"); else { System.out.println(e.getMessage()); } }
finally { JOptionPane.showConfirmDialog(null,"OK"); } } }

執行結果:

2.多層的異常捕獲-1

閱讀以下程式碼(CatchWho.java),寫出程式執行結果。

public class CatchWho { 
    public static void main(String[] args) { 
        try { 
                try { 
                    throw new ArrayIndexOutOfBoundsException(); 
                } 
                
catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); } throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }

執行結果:

分析:程式執行到第五行時丟擲ArrayIndexOutOfBoundsException();該異常被第16行的catch語句捕獲,

多層的異常捕獲-2

寫出CatchWho2.java程式執行的結果

public class CatchWho2 { 
    public static void main(String[] args) { 
        try {
                try { 
                    throw new ArrayIndexOutOfBoundsException(); 
                } 
                catch(ArithmeticException e) { 
                    System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); 
                }
            throw new ArithmeticException(); 
        } 
        catch(ArithmeticException e) { 
            System.out.println("發生ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) { 
            System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); 
        } 
    } 
}

執行結果:

分析:程式執行到第5行時丟擲ArrayIndexOutOfBoundsException();該異常被第15行的catch語句捕獲

總結:

(1).異常捕獲時可以有多個catch語句塊,每個程式碼塊捕獲一種異常。

(2).在某個try塊後有兩個不同的catch 塊捕獲兩個相同型別的異常是語法錯誤。

(3).使用catch語句,只能捕獲Exception類及其子類的物件。因此,一個捕獲Exception物件的catch語句塊可以捕獲所有“可捕獲”的異常。

(4).將catch(Exception e)放在別的catch塊前面會使這些catch塊都不執行,因此Java不會編譯這個程式。

3.當有多個巢狀的try...catch....finally時,要特別注意finally的執行時機,請先閱讀EmbedFinally.java示例,再執行它,觀察其輸出再進行總結。

 

public class EmbededFinally {

    
    public static void main(String args[]) {
        
        int result;
        
        try {
            
            System.out.println("in Level 1");

           
             try {
                
                System.out.println("in Level 2");
  // result=100/0;  //Level 2
               
                 try {
                   
                     System.out.println("in Level 3");
                      
                     result=100/0;  //Level 3
                
                } 
                
                catch (Exception e) {
                    
                    System.out.println("Level 3:" + e.getClass().toString());
                
                }
                
                
                finally {
                    
                    System.out.println("In Level 3 finally");
                
                }
                
               
                // result=100/0;  //Level 2

            
                }
            
            catch (Exception e) {
               
                 System.out.println("Level 2:" + e.getClass().toString());
           
             }
             finally {
                
                System.out.println("In Level 2 finally");
           
             }
             
            // result = 100 / 0;  //level 1
        
        } 
        
        catch (Exception e) {
            
            System.out.println("Level 1:" + e.getClass().toString());
        
        }
        
        finally {
           
             System.out.println("In Level 1 finally");
        
        }
    
    }

}

結果:

特別注意:當有多層巢狀的finally時,異常在不同層次丟擲,在不同的位置丟擲,可能導致不同的finally語句執行順序

4.finally語句塊一定會執行嗎?請通過SystemExitAndFinally.java示例程式回答上述問題。

public class SystemExitAndFinally {

    
    public static void main(String[] args)
    {
        
        try{

            
            System.out.println("in main");
            
            throw new Exception("Exception is thrown in main");

                    //System.exit(0);

        
        }
        
        catch(Exception e)

            {
            
            System.out.println(e.getMessage());
            
            System.exit(0);
        
        }
        
        finally
        
        {
            
            System.out.println("in finally");
        
        }
    
    }


}

結果:

分析總結:

(1)try語句沒有被執行到,如在try語句之前return就返回了,這樣finally語句就不會執行。這也說明了finally語句被執行的必要而非充分條件是:相應的try語句一定被執行到。

(2)在try塊|catch塊中有System.exit(0);這樣的語句。System.exit(0)是終止Java虛擬機器JVM的,連JVM都停止了,所有都結束了,當然finally語句也不會被執行到。