天天看點

如何抛出異常,自定義異常

https://blog.csdn.net/qq_18505715/article/details/73196421

一、異常的抛出

1、定義 : 一個方法不處理這個異常,而是調用層次向上傳遞,誰調用這個方法,這個異常就由誰來處理。

2、throw : 将産生的異常抛出(強調的是動作),抛出的既可以是異常的引用,也可以是異常對象。(位置: 方法體内)

3、throws : 如果一個方法可能會出現異常,但沒有能力處理這種異常,可以在方法聲明處用throws子句來聲明抛出異常。用它修飾的方法向調用者表明該方法可能會抛出異常(可以是一種類型,也可以是多種類型,用逗号隔開)(位置:寫在方法名 或方法名清單之後 ,在方法體之前。)

注意 : 調用可能會抛出異常的方法,必須添加try-catch代碼塊嘗試去捕獲異常 或者 添加throws 聲明 來将異常 抛出給更上一層的調用者進行處理,這裡需要注意一個細節:新的異常包含原始異常的所有資訊,根據這個我們可以去追溯最初異常發生的位置,

如下圖所示 

如何抛出異常,自定義異常

4、簡單使用

// 定義一個方法,抛出 數組越界和算術異常(多個異常 用 "," 隔開)
    public void Test1(int x) throws ArrayIndexOutOfBoundsException,ArithmeticException{

    System.out.println(x);

    if(x == ){

        System.out.println("沒有異常");
        return;
    }

    //資料越界異常
    else if (x == ){

        int[] a = new int[];
         a[] = ;
    }

    //算術異常
    else if (x == ){

        int i = ;
        int j = /;
    }

    }
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

在main方法中調用

public static void main(String[] args) {

        //建立對象
        ExceptionInital object = new ExceptionInital();

        // 調用會抛出異常的方法,用try-catch塊
        try{

            object.Test1();

        }catch(Exception e){

            System.out.println(e);
        }

        // 數組越界異常
        try{

            object.Test1();
        }catch (ArrayIndexOutOfBoundsException e) {

            System.out.println("數組越界異常:"+e);
        }


        // 算術異常
        try{

            object.Test1();

        }catch(ArithmeticException e){

            System.out.println("算術異常:"+e);
        }


        //使用 throw 抛出異常(可以抛出異常對象,也可以抛出異常對象的引用)
        try{

            ArrayIndexOutOfBoundsException  exception = new ArrayIndexOutOfBoundsException();

            throw exception;//new ArrayIndexOutOfBoundsException();

        }catch(ArrayIndexOutOfBoundsException e){

            System.out.println("thorw抛出異常:"+e);
        }

    }
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

運作結果 

如何抛出異常,自定義異常

總結下 throw 和throws 關鍵字的差別

1、寫法上 : throw 在方法體内使用,throws 函數名後或者參數清單後方法體前 

2、意義 : throw 強調動作,而throws 表示一種傾向、可能但不一定實際發生 

3、throws 後面跟的是異常類,可以一個,可以多個,多個用逗号隔開。throw 後跟的是異常對象,或者異常對象的引用。 

4、throws 使用者抛出異常,當在目前方法中抛出異常後,目前方法執行結束(throws 後,如果有finally語句的話,會執行到finally語句後再結束。)。可以了解成return一樣。

二、自定義異常

前面所講的異常,都是系統自帶的,系統自己處理,但是很多時候項目會出現特有問題,而這些問題并未被java所描述并封裝成對象,是以對于這些特有的問題可以按照java的對問題封裝的思想,将特有的問題進行自定義異常封裝。在Java中要想建立自定義異常,需要繼承Throwable或者他的子類Exception。

文法

class  自定義異常類 extends 異常類型(Exception){

 // 因為父類已經把異常資訊的操作都完成了,所在子類隻要在構造時,将異常資訊傳遞給父類通過super 語句即可。
  // 重寫 有參 和 無參  構造方法
}
           
  • 1
  • 2
  • 3
  • 4
  • 5

例如:

public class CustomException extends Exception {

    //無參構造方法
    public CustomException(){

        super();
    }

    //有參的構造方法
    public CustomException(String message){
        super(message);

    }

    // 用指定的詳細資訊和原因構造一個新的異常
    public CustomException(String message, Throwable cause){

        super(message,cause);
    }

    //用指定原因構造一個新的異常
     public CustomException(Throwable cause) {

         super(cause);
     }

}

// 備注: 這些方法怎麼來的? 重寫父類Exception的方法,那麼如何檢視Exception具有哪些API,快捷鍵:選中Exception, command+單擊。windows系統 :選中Exception, control+單擊。
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

自定義異常的使用例子: 

自定義test1()方法,抛出 “我喝酒了”的異常資訊,test2()方法調用test1()方法,并将異常包裝成RuntimeException類型的異常,繼續抛出,在main方法中調用test2()方法,并嘗試捕獲異常

public void test2() {

        try{

            test1();

        }catch (CustomException e){

           RuntimeException exception  =  new RuntimeException(e);
           //exception.initCause(cause)
           throw  exception;
        }

    }

    public void test1() throws CustomException{

        throw new CustomException("我喝酒了");
    }
// main方法
    public static void main(String[] args) {

        CustomExceptionInital object =  new  CustomExceptionInital();

        //try{

            object.test2();

        //}catch(Exception e){

        //e.printStackTrace();

        //}

    }
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

輸出結果: 

如何抛出異常,自定義異常