Java基礎--異常--對多異常的處理

來源:互聯網
上載者:User

標籤:

對多異常的處理

1、聲明異常時,建議聲明更為具體的異常,這樣處理的可以更加具有針對性

2、對方聲明幾個異常,就應該對應有幾個catch塊,不要定義多餘的catch塊

如果多個catch塊中的異常出現繼承關係,父類異常catch塊放在最下面。

 

注意:

/* 建立在進行catch處理時,catch中一定要定義具體處理方式。

不要簡單定義一句 e.printStackTrace(),----因為使用者看不懂

也不要簡單地書寫一條輸出語句 */

真發生問題,在catch裡面建立、記錄問題到記錄檔中,供維護人員查看。

--------------------------

ExceptionTest.java

public class ExceptionTest {    public static void main(String[] args){        DivDemo dd =new DivDemo();                int resultD;        try {            resultD = dd.div(10, 0);            System.out.println("resultD‘ value is :"+resultD);        } catch (ArithmeticException e) {            e.printStackTrace();        } catch (ArrayIndexOutOfBoundsException e) {            e.printStackTrace();        } catch (Exception e){            e.printStackTrace();        }        System.out.println("over!");    }}class DivDemo{    public int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException {        int [] arr = new int[5];                System.out.println(arr[a]);        return a/b;       }}

:console:

Java基礎--異常--對多異常的處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.