異常處理的方式

處理異常的方式:
對于編譯期異常處理有兩種不同的處理方式。
(1) 使用try{ } catch { } finally 語句塊處理它。
(2)在函數(shù)簽名中使用throws 聲明交給函數(shù)調(diào)用者去解決。

try catch finally 塊是異常的捕獲,其本質(zhì)是判斷,基本語法如下:

try{
    可能出現(xiàn)異常的代碼(包括不會出現(xiàn)異常的代碼)
   } catch( Exception e) { // 括號里面接收try代碼塊中出現(xiàn)的異常類型
    如果出現(xiàn)異常時的處理代碼
   
   }finally {
    不管代碼是正常執(zhí)行還是出現(xiàn)異常需要處理,finally代碼塊中的代碼最終都會執(zhí)行
}

自定義異常
格式:
(1)自定義一個編譯器異常:自定義類并繼承java.lang.Exception。
(2)自定義一個運行時期的異常類:自定義類,并繼承于java.lang.RuntimeException。

public class 自定義異常類 extends java.langException{
}

使用時需要輸出異常信息,這需要調(diào)用父類的構(gòu)造方法。

public class PersonException extends Exception{
    private static final long serialVersionUID = 1L;
    
    public PersonException(){
        super();
    }
    
    
    public PersonException(String message){
        super(message);
    }
}

使用自定義異常:

public void savePerson(Person person) throws PesonException {
   
   if( null = = person.getPId()(){
      throw new PersonException("沒有Person編號,請檢查");
   }
}

注意:

(1)自定義異常類一般是Exception結(jié)尾的。

(2) 自定義異常類,必須繼承Exception或者RuntimeException。

1)繼承Exception,那么自定義的異常類就是一個編譯期異常,如果方法內(nèi)部拋出了編譯期異常,就必須處理這個異常,要么throws,要么try...catch。

2)繼承RuntimeException,那么自定義的異常類就是一個運行期異常,無法處理,交給虛擬機(jī)處理(中斷處理)。

Dome01:

public class Dome01 {
    public static void main(String[] args) {
        int divisor  = 10 ;
        int dividend = 0 ;
        // System.out.println(divisor/dividend);  // ArithmeticException  算術(shù)異常
        try {
            System.out.println(divisor / dividend);  //ArithmeticException 算術(shù)異常
        }catch (Exception e ) {
            e.printStackTrace();
            System.out.println("捕獲到一個異常");
        }finally {
            System.out.println("不管如何都會執(zhí)行這里的代碼");
        }
        System.out.println("哈哈哈哈哈哈");

    }
}

Dome02:

public class Dome02 {
    public static void main(String[] args) {
        int[] a = new int[2];
        Scanner scanner = new Scanner(System.in);
        try {
            int i = scanner.nextInt();
            int j = scanner.nextInt();
            a[0] =i;
            a[2] = j;
            System.out.println( a[0] / a[2]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("數(shù)據(jù)越界異常");
        } catch (NumberFormatException e) {
            System.out.println("數(shù)據(jù)格式不正確異常");
        } catch (ArithmeticException e) {
            System.out.println("算數(shù)異常");
        }
    }
}

Dome03:

public class Dome03 {
    public static void main(String[] args) {
        int[] a = new int[2];
        Scanner scanner = new Scanner(System.in);
        try {
            int i = scanner.nextInt();
            int j = scanner.nextInt();
            a[0] = i;
            a[2] = j;
            System.out.println( a[0] / a[2]);
            // Array Index OutOf Bounds Exception  數(shù)組 索引 超出 邊界 異常
            // Input Mismatch Exception  輸入 不匹配 異常
            // Arithmetic Exception  數(shù)學(xué)數(shù)字 異常
        } catch (ArrayIndexOutOfBoundsException | InputMismatchException | ArithmeticException e) {
            System.out.println("數(shù)據(jù)越界異常");
            System.out.println("數(shù)據(jù)格式不正確異常");
            System.out.println("算數(shù)異常");
            System.out.println("以上異常中的一個");
        }

    }
}

Dome04:

public class Dome04 {
    public static void main(String[] args) throws Exception{       //繼續(xù)向上聲明,不處理
        /*try {
            steSex("雙性人");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("非男非女");
        }*/
        steSex("afwarf");
    }
    public static void steSex(String sex) throws Exception{  //聲明異常
        if (!(sex.equals("男")|| sex.equals("女"))){
            throw new Exception("處理不了的異常,扔出去");  //拋出異常
        }
    }
}

Dome05

public class Dome05 {
    public static void main(String[] args){
        try {
            steSex("雙性人");
        }catch (Exception e){
            System.out.println("調(diào)用者說處理過了");
        }
    }
    public static void steSex(String sex) throws SexException{  //聲明異常
        if (!(sex.equals("男")|| sex.equals("女"))){
            throw new SexException("發(fā)現(xiàn)一個不對勁的");  //拋出異常
        }
    }
}

SexException:

public class SexException extends Exception {
    public SexException(){
    }
    public SexException(String message){
        System.out.println(message);
        System.out.println("自定義的異常,知道非男非女,但是沒辦法處理");
        System.out.println("..........");
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容