1. 程式人生 > >異常處理的動手動腦

異常處理的動手動腦

一,拋異常的型別

拋異常:1,Error錯誤,系統錯誤 2,Exception錯誤
Exception類又包括:RuntimeException非檢查異常和檢查異常。
Error例子: 1.AssertionError程式執行期間判斷某個條件是否滿足
,不滿足時,丟擲AssertionError。

                 2.OOM Error系統記憶體不足。

二,小探索

int i=1, j=0, k;
k=i/j;


javac生成idiv位元組碼指令
double d1=100,d2=0,result;
    result=d1/d2;
System.out.println("浮點數除以零:" + data);

浮點數除以0:Infinity
javac生成ddiv位元組碼指令,JVM在具體實現這兩個指令時,採用了不同的處理策略,導致兩段程式碼執行時得到不同的結果。

三,try catch的巢狀

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/內層try-catch 發生ArithmeticException

上述是結果

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"); 
        } 
    } 
}
ArrayIndexOutOfBoundsException/外層try-catch

看下面這段程式碼,

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

}

執行結果為

in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

當有多層巢狀的finally時,異常在不同的層次丟擲    ,在不同的位置丟擲,會導致不同的finally語句塊執行順序。

四,自定義異常

Class MyException extends Exception
    { …    }
Class MyClass {
        void someMethod() {
            if (條件) throw new MyException();
        }
    }