Docker 的優點
758
2025-04-02
一、異常
1.異常
異常的概述
異常就是程序出現了不正常的情況
異常的體系結構
2.編譯時異常和運行時異常的區別
編譯時異常
都是Exception類及其子類
必須顯示處理,否則程序就會發生錯誤,無法通過編譯
運行時異常
都是RuntimeException類及其子類
無需顯示處理,也可以和編譯時異常一樣處理
圖示
3.JVM默認處理異常的方式
如果程序出現了問題,我們沒有做任何處理,最終JVM 會做默認的處理,處理方式有如下兩個步驟:
把異常的名稱,錯誤原因及異常出現的位置等信息輸出在了控制臺
程序停止執行
4.查看異常信息
控制臺在打印異常信息時,會打印異常類名,異常出現的原因,異常出現的位置
我們調bug時,可以根據提示,找到異常出現的位置,分析原因,修改異常代碼
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-sEoETkej-1641394789269)(img\03_查看異常信息.png)]
5.throws方式處理異常
定義格式
public void 方法() throws 異常類名 { }
示例代碼
public class ExceptionDemo { public static void main(String[] args) throws ParseException{ System.out.println("開始"); // method(); method2(); System.out.println("結束"); } //編譯時異常 public static void method2() throws ParseException { String s = "2048-08-09"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d = sdf.parse(s); System.out.println(d); } //運行時異常 public static void method() throws ArrayIndexOutOfBoundsException { int[] arr = {1, 2, 3}; System.out.println(arr[3]); } }
注意事項
這個throws格式是跟在方法的括號后面的
編譯時異常必須要進行處理,兩種處理方案:try…catch …或者 throws,如果采用 throws 這種方案,在方法上進行顯示聲明,將來誰調用這個方法誰處理
運行時異常因為在運行時才會發生,所以在方法后面可以不寫,運行時出現異常默認交給jvm處理
6.throw拋出異常
格式
throw new 異常();
注意
這個格式是在方法內的,表示當前代碼手動拋出一個異常,下面的代碼不用再執行了
throws和throw的區別
示例代碼
public class ExceptionDemo8 { public static void main(String[] args) { //int [] arr = {1,2,3,4,5}; int [] arr = null; printArr(arr);//就會 接收到一個異常. //我們還需要自己處理一下異常. } private static void printArr(int[] arr) { if(arr == null){ //調用者知道成功打印了嗎? //System.out.println("參數不能為null"); throw new NullPointerException(); //當參數為null的時候 //手動創建了一個異常對象,拋給了調用者,產生了一個異常 }else{ for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } } }
7.try-catch方式處理異常
定義格式
try { 可能出現異常的代碼; } catch(異常類名 變量名) { 異常的處理代碼; }
執行流程
程序從 try 里面的代碼開始執行
出現異常,就會跳轉到對應的 catch 里面去執行
執行完畢之后,程序還可以繼續往下執行
示例代碼
public class ExceptionDemo01 { public static void main(String[] args) { System.out.println("開始"); method(); System.out.println("結束"); } public static void method() { try { int[] arr = {1, 2, 3}; System.out.println(arr[3]); System.out.println("這里能夠訪問到嗎"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("你訪問的數組索引不存在,請回去修改為正確的索引"); } } }
注意
如果 try 中沒有遇到問題,怎么執行?
會把try中所有的代碼全部執行完畢,不會執行catch里面的代碼
如果 try 中遇到了問題,那么 try 下面的代碼還會執行嗎?
那么直接跳轉到對應的catch語句中,try下面的代碼就不會再執行了
當catch里面的語句全部執行完畢,表示整個體系全部執行完全,繼續執行下面的代碼
如果出現的問題沒有被捕獲,那么程序如何運行?
那么try…catch就相當于沒有寫.那么也就是自己沒有處理.
默認交給虛擬機處理.
同時有可能出現多個異常怎么處理?
出現多個異常,那么就寫多個catch就可以了.
注意點:如果多個異常之間存在子父類關系.那么父類一定要寫在下面
8.Throwable成員方法
常用方法
示例代碼
public class ExceptionDemo02 { public static void main(String[] args) { System.out.println("開始"); method(); System.out.println("結束"); } public static void method() { try { int[] arr = {1, 2, 3}; System.out.println(arr[3]); //new ArrayIndexOutOfBoundsException(); System.out.println("這里能夠訪問到嗎"); } catch (ArrayIndexOutOfBoundsException e) { //new ArrayIndexOutOfBoundsException(); // e.printStackTrace(); //public String getMessage():返回此 throwable 的詳細消息字符串 // System.out.println(e.getMessage()); //Index 3 out of bounds for length 3 //public String toString():返回此可拋出的簡短描述 // System.out.println(e.toString()); //java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 //public void printStackTrace():把異常的錯誤信息輸出在控制臺 e.printStackTrace(); // java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3 // at com.itheima_02.ExceptionDemo02.method(ExceptionDemo02.java:18) // at com.itheima_02.ExceptionDemo02.main(ExceptionDemo02.java:11) } } }
9.異常的練習
需求
鍵盤錄入學生的姓名和年齡,其中年齡為18 - 25歲,超出這個范圍是異常數據不能賦值.需要重新錄入,一直錄到正確為止
實現步驟
創建學生對象
鍵盤錄入姓名和年齡,并賦值給學生對象
如果是非法數據就再次錄入
代碼實現
學生類
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age >= 18 && age <= 25){ this.age = age; }else{ //當年齡不合法時,產生一個異常 throw new RuntimeException("年齡超出了范圍"); } } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
測試類
public class ExceptionDemo12 { public static void main(String[] args) { // 鍵盤錄入學生的姓名和年齡,其中年齡為 18 - 25歲, // 超出這個范圍是異常數據不能賦值.需要重新錄入,一直錄到正確為止。 Student s = new Student(); Scanner sc = new Scanner(System.in); System.out.println("請輸入姓名"); String name = sc.nextLine(); s.setName(name); while(true){ System.out.println("請輸入年齡"); String ageStr = sc.nextLine(); try { int age = Integer.parseInt(ageStr); s.setAge(age); break; } catch (NumberFormatException e) { System.out.println("請輸入一個整數"); continue; } catch (AgeOutOfBoundsException e) { System.out.println(e.toString()); System.out.println("請輸入一個符合范圍的年齡"); continue; } /*if(age >= 18 && age <=25){ s.setAge(age); break; }else{ System.out.println("請輸入符合要求的年齡"); continue; }*/ } System.out.println(s); } }
10.自定義異常
自定義異常概述
當Java中提供的異常不能滿足我們的需求時,我們可以自定義異常
實現步驟
定義異常類
寫繼承關系
提供空參構造
提供帶參構造
代碼實現
異常類
public class AgeOutOfBoundsException extends RuntimeException { public AgeOutOfBoundsException() { } public AgeOutOfBoundsException(String message) { super(message); } }
學生類
public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if(age >= 18 && age <= 25){ this.age = age; }else{ //如果Java中提供的異常不能滿足我們的需求,我們可以使用自定義的異常 throw new AgeOutOfBoundsException("年齡超出了范圍"); } } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
測試類
public class ExceptionDemo12 { public static void main(String[] args) { // 鍵盤錄入學生的姓名和年齡,其中年齡為 18 - 25歲, // 超出這個范圍是異常數據不能賦值.需要重新錄入,一直錄到正確為止。 Student s = new Student(); Scanner sc = new Scanner(System.in); System.out.println("請輸入姓名"); String name = sc.nextLine(); s.setName(name); while(true){ System.out.println("請輸入年齡"); String ageStr = sc.nextLine(); try { int age = Integer.parseInt(ageStr); s.setAge(age); break; } catch (NumberFormatException e) { System.out.println("請輸入一個整數"); continue; } catch (AgeOutOfBoundsException e) { System.out.println(e.toString()); System.out.println("請輸入一個符合范圍的年齡"); continue; } /*if(age >= 18 && age <=25){ s.setAge(age); break; }else{ System.out.println("請輸入符合要求的年齡"); continue; }*/ } System.out.println(s); } }
5G教育 Java
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。