19Java基礎之異常
異常
什么是異常?
- 異常就是代表程序出現的問題。
常見的異常
//目標:認識異常
public class ExceptionDemo01 {
public static void main(String[] args) throws ParseException {
int[] arr = {1, 2, 3};
System.out.println("程序開始");
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
// System.out.println(arr[3]);// 報ArrayIndexOutOfBoundsException異常
System.out.println("程序結束");
String s = null;
// System.out.println(s.length());// 報NullPointerException異常,空指針異常
// System.out.println(10/0); //報ArithmeticException異常,算術異常
Object o = "張麻子";
Integer i = (Integer) o;
// System.out.println(i); // 報ClassCastException異常,類型轉換異常
String s1 = "23a";
int it = Integer.valueOf(s1);
// System.out.println(it); // 報NumberFormatException異常,數字格式化異常
parseDate("2025-08-06 21:22:23");
}
public static void parseDate(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(date); // 編譯時錯誤,寫代碼時就會報錯。
}
}

-
Error:代表的系統級別錯誤(屬于嚴重問題),也就是說系統一旦出現問題,sun公司會把這些問題封裝成Error對象給出來,說白了,Error是給sun公司自己用的,不是給我們程序員用的,因此我們開發人員不用管它。
-
Exception:叫異常,它代表的才是我們程序可能出現的問題,所以,我們程序員通常會用Exception以及它的孩子來封裝程序出現的問題。
- 運行時異常:RuntimeException及其子類,編譯階段不會出現錯誤提醒,運行時出現的異常(如數組索引越界異常)
- 編譯時異常:編譯階段就會出現錯誤提醒的。(如:日期解析異常)
異常處理方式
- 拋出異常(throws)
- 在方法上使用throws關鍵字,可以將方法內部出現的異常拋出去給調用者處理。
- 格式:
方法 throws 異常1,異常2,異常3{
...
}
- 捕獲異常(try...catch)
- 直接捕獲程序出現的異常。
- 格式:
try{
// 監視可能出現異常的代碼!
}catch(異常類型1 變量){
// 處理異常
}catch(異常類型2 變量){
// 處理異常
}
異常有什么作用?
- 異常是用來查詢系統Bug的關鍵參考信息!
- 異常可以作為方法內部的一種特殊返回值,以便通知上層調用者底層的執行情況。
案例:
// 目標:搞清楚異常的作用。
public class ExceptionDemo02 {
public static void main(String[] args) {
try {
// 監視代碼
divide(2, 1);
System.out.println("成功了!");
} catch (Exception e) {
System.out.println("失敗了!");
// 捕獲異常,并打印出異常的信息。
e.printStackTrace();
}
}
public static int divide(int a, int b){
if(b == 0){
System.out.println("參數有問題!");
// return -1;
// 拋出一個異常作為返回值,通知上層這里出現了bug。
throw new RuntimeException("/ by 0!");
}
int c = a / b;
return c;
}
}
小技巧
- 在IDEA中,在要拋出異常的語句上按ctrl+ alt + T,從彈出的下拉表單中找到try/catch,回車,就可以直接生成異常捕獲。
自定義異常
- Java無法為這個世界上全部的問題都提供異常類來代表,如果企業自己的某種問題,想通過異常來表示,以便用異常來管理該問題,那就需要自己來定義異常類。
自定義異常的種類
- 自定義運行時異常
- 自定義編譯時異常
![image]()
注意:
- 自定義異常時,建議使用自定義運行時異常。這種異常對程序員干擾性少,如果定義編譯時異常,連鎖反應太多,容易干擾程序員。
- 要注意throw和throws的區別。throw是方法內部使用,創建異常并從此點拋出異常;throws是在方法上,拋出方法內部的異常給調用者。
案例:
自定義運行時異常類:
// 自定義運行時異常
/*
* 1. 繼承RuntimeException
* 2. 重寫構造器
* 3. 通過throw new 自定義異常對象拋出異常
* */
public class AgeIllegalRuntimeException extends RuntimeException {
public AgeIllegalRuntimeException() {
}
public AgeIllegalRuntimeException(String message) {
super(message);
}
}
自定義異常類:
// 自定義編譯時異常
/*
* 1. 繼承Exception
* 2. 重寫構造器
* 3. 通過throw new 自定義異常對象拋出異常
* */
public class AgeIllegalException extends Exception {
public AgeIllegalException() {
}
public AgeIllegalException(String message) {
super(message);
}
}
測試:
//目標:自定義異常
public class ExceptionDemo04 {
public static void main(String[] args) throws AgeIllegalException {
System.out.println("start...");
try {
save(100);
System.out.println("執行成功!");
} catch (AgeIllegalException e) {
e.printStackTrace();
System.out.println("執行失敗!");
}
System.out.println("end...");
}
public static void save(int age) throws AgeIllegalException {
// throw 方法內部使用,創建異常并從此點拋出異常
// throws 方法上,拋出方法內部的異常給調用者。
if(age <= 0 || age > 65){
//這個年齡非法!創建異常時對象來拋出異常
throw new AgeIllegalException("年齡非法!");
}
System.out.println("年齡合法:" + age);
}
}
異常的常見處理方式

- 捕獲異常,記錄異常并響應合適的信息給用戶。(推薦)
- 捕獲異常,嘗試重新修復。


浙公網安備 33010602011771號