e.getMessage() e.printStackTrace() 和e.printStackTrace() 小結

來源:互聯網
上載者:User

標籤:str   pointer   pre   out   tostring   ack   tst   種類   java   

1 e.getMessage() ;    只會獲得異常的名稱。比如說NullPoint null 指標,就告訴你說是null 指標

2.e.toString():  獲得異常種類和錯誤資訊

3.e.printStackTrace();    在控制台列印列印出詳細的異常資訊,異常名稱。便於調試

3 如果異常是用無參構造器構造後續又沒有設定message的話一般就是null,如果用帶String參數的構造器構造的話,返回的就是那個參數。

下面是栗子1:

    public static String str =null;    public static void main( String[] args )    {        try{            if(str.equals("name")){}        }catch (Exception e){            System.out.println("e.toString()列印結果: "+ e.toString());            System.out.println("e.getMessage()列印結果: "+ e.getMessage());            System.out.println("============分割線=============");            e.printStackTrace();        }    }

執行結果:

e.toString()列印結果: java.lang.NullPointerException
e.getMessage()列印結果: null
============分割線=============

java.lang.NullPointerException
at test.App.main(App.java:13)

下面是栗子2:

   public static int m = 0;    public static void main( String[] args )    {        try{            m = 899/0;        }catch (Exception e){            System.out.println("e.toString()列印結果: "+ e.toString());            System.out.println("e.getMessage()列印結果: "+ e.getMessage());            System.out.println("============分割線=============");            e.printStackTrace();        }    }

執行結果:
e.toString()列印結果: java.lang.ArithmeticException: / by zero
e.getMessage()列印結果: / by zero
============分割線=============
java.lang.ArithmeticException: / by zero
at test.App2.main(App2.java:13)

  

e.getMessage() e.printStackTrace() 和e.printStackTrace() 小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.