標籤:
簡介:
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類的一些方法