1. 程式人生 > >第8周動手動腦

第8周動手動腦

1.多層的異常捕獲-1

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"); } } }

兩個try-catch異常捕獲,第一個throw丟擲的錯誤,被內層catch捕獲,故最後一個catch未捕獲,不顯示;第二個catchArithmeticException,被同名即第二個catch捕獲,顯示發生ArithmeticException。

多層的異常捕獲-2

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"); } } }

當第一個throw丟擲錯誤後,直接跳轉到最後一個同名的catch捕獲塊,中間程式未執行。故總結,Java中,使用try-catch語法,一旦出錯,就捕獲該錯誤;若登出第一個throw錯誤,則會執行第二個catch,顯示發生ArithmeticException。

2.Embedefinally

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");
        
        }
    
    }

}

共三個try-catch-finally巢狀,每個try、catch、finally均有輸出語句。輸出順序為從第一個try開始執行三次,catch僅執行最裡層level3,finally從最裡層向外執行。

Finally主要用於解決資源洩露問題,它位於catch語句塊後,JVM保證它一定執行,因此從最裡層執行,毫無疑問。

由於finally語塊中可能發生異常,比如此處的level3就發生java.lang.ArithmeticException異常,一旦發生此種異常,先前異常就會被拋棄,故僅僅最裡層的catch捕獲到異常,之後由於異常被拋棄,level2、level3的catch並未捕捉到異常不顯示。

另外根據try-catch方法使用,try語句塊一有異常,則找相應catch捕獲經驗得知,三個try中均為異常錯誤,故依次執行try中語句塊。

3.SystemExitAndFinally

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");
        
        }
    
    }


}

通常情況下,finally執行語句一定執行,但本題中有特殊情況,在catch中有“System.exit(0);”執行此語句後,就已經結束程式,故不會執行finally語句。

4.