java異常的概述和分類
- A:異常的概述
- 異常就是Java程序在運行過程中出現的錯誤。
- B:異常的分類
- 通過API查看Throwable
- Error
- 服務器宕機,數據庫崩潰等
- Exception
C:異常的繼承體系 - Throwable
- Error
- Exception
- RuntimeException
java中JVM默認是如何處理異常的
- A:JVM默認是如何處理異常的
- main函數收到這個問題時,有兩種處理方式:
- a:自己將該問題處理,然後繼續運行
- b:自己沒有針對的處理方式,只有交給調用main的jvm來處理
- jvm有一個默認的異常處理機制,就將該異常進行處理.
- 並將該異常的名稱,異常的信息.異常出現的位置打印在了控制檯上,同時將程序
java中異常的處理方式
- A:異常處理的兩種方式
- a:try…catch…finally
- try catch
- try catch finally
- try finally
- b:throws
- a:try…catch…finally
- B:try…catch處理異常的基本格式
- try…catch…finally
案例演示
public class Demo2_Exception {
/**
* * A:異常處理的兩種方式
* a:try…catch…finally
* try catch
* try catch finally
* try finally
* b:throws
* B:try...catch處理異常的基本格式
* try…catch…finally
* C:案例演示
* try...catch的方式處理1個異常
try:用來檢測異常的
catch:用來捕獲異常的
finally:釋放資源
世界上最真情的相依就是你在try我在catch,無論你發神馬脾氣,我都靜靜接受,默默處理
當通過trycatch將問題處理了,程序會繼續執行
*/
public static void main(String[] args) {
// TODO 自動生成的方法存根
int a=10;
int b=0;
String[] arr = {"ds","cxs","sca","csd"};
try {
System.out.println(a/b); //除0異常
System.out.println(arr[10]); //索引越界異常
arr=null;
System.out.println(arr[0]);
}
catch (ArithmeticException e) { //Exception e = new ArithmeticException();
System.out.println("除數不能爲0!");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("數組中索引越界了!");
}
catch (Exception e) { //Exception e = new NullPointerException();
System.err.println("代碼有錯誤!");
}finally {
System.out.println("不論是否發生異常,該語句都會執行。");
}
}
}