Java基礎-異常類--異常概念、一般格式、Throwable類的一些方法

來源:互聯網
上載者:User

標籤:

簡介:

1、異常的概念

異常:問題-->不正常情況---》封裝成對象 ;

java對不同的不正常情況進行描述後的對象體現;

 

對於嚴重的異常,java通過Error類進行描述-------一般不編寫針對性的代碼對其進行處理

對於非嚴重可處理的異常的,java通過Exception類進行描述--可使用針對性的代碼進行處理

 

體系: Object<--Throwable<--Error/Exception

2、一般格式

try{ * * }

catch(異常類 變數){ * 處理問題* }

finally{ * 一定會執行的語句; * }

3、Throwable類的一些方法

String getMessage()

Returns the detail message string of this throwable.

void printStackTrace()

Prints this throwable and its backtrace to the standard error stream.

String toString()

Returns a short description of this throwable.

ExceptionTest.java
public class ExceptionTest {    public static void main(String[] args){        //檢測        try{            int resultD = DivDemo.div(10, 0);            System.out.println("resultD‘value is :"+resultD);        }        //接收截獲異常對象,然後處理問題,處理完後往下執行。        catch(Exception e){                    System.out.println("Get it: "+e.getMessage());            System.out.println("Get it: "+e.toString());            e.printStackTrace();  
            //列印三個資訊----異常名稱,異常資訊,異常出現的位置                  
            //其實jvm預設的異常處理機制:就是在調用printStackTrace()方法            //列印異常的堆棧的跟蹤資訊        }                System.out.println("over!");    }}class DivDemo{        public static int div(int a,int b){        return a/b;       }}

:console:

Java基礎-異常類--異常概念、一般格式、Throwable類的一些方法

聯繫我們

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