一、例外體系結構
1.結構
java.lang.Throwable
-
java.lang.Error:一般不撰寫針對性的代碼進行處理,
-
java.lang.Exception:可以進行例外的處理
- 編譯時例外(checked)
- IOException
- FileNotFoundException
- ClassNotFoundException
- IOException
- 運行時例外(unchecked,RuntimeException)
- NullPointerException
- ArrayIndexOutOfBoundsException
- ClassCastException
- NumberFormatException
- InputMismatchException
- ArithmeticException
- 編譯時例外(checked)

2.從程式執行程序,看編譯時例外和運行時例外

編譯時例外:執行javac.exe命名時,可能出現的例外,
運行時例外:執行java.exe命名時,出現的例外,
3.常見的例外型別
//******************以下是運行時例外***************************
//ArithmeticException
@Test
public void test6(){
int a = 10;
int b = 0;
System.out.println(a / b);
}
//InputMismatchException
@Test
public void test5(){
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
scanner.close();
}
//NumberFormatException
@Test
public void test4(){
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
//ClassCastException
@Test
public void test3(){
Object obj = new Date();
String str = (String)obj;
}
//IndexOutOfBoundsException
@Test
public void test2(){
//ArrayIndexOutOfBoundsException
//int[] arr = new int[10];
//System.out.println(arr[10]);
//StringIndexOutOfBoundsException
String str = "abc";
System.out.println(str.charAt(3));
}
//NullPointerException
@Test
public void test1(){
//int[] arr = null;
//System.out.println(arr[3]);
String str = "abc";
str = null;
System.out.println(str.charAt(0));
}
//******************以下是編譯時例外***************************
@Test
public void test7(){
//File file = new File("hello.txt");
//FileInputStream fis = new FileInputStream(file);
//
//int data = https://www.cnblogs.com/java-blogs/archive/2022/02/24/fis.read();
//while(data != -1){
//System.out.print((char)data);
//data = fis.read();
//}
//
//fis.close();
}
二、自定義例外類
1.自定義一個例外類
/*
* 1. 繼承于現的例外結構:RuntimeException 、Exception
* 2. 提供全域常量:serialVersionUID
* 3. 提供多載的構造器
*
*/
public class MyException extends Exception{
static final long serialVersionUID = -7034897193246939L;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}
三、例外的處理
1.java例外處理的抓拋模型
程序一:"拋":程式在正常執行的程序中,一旦出現例外,就會在例外代碼處生成一個對應例外類的物件,并將此物件拋出,
一旦拋出物件以后,其后的代碼就不再執行,
關于例外物件的產生:①系統自動生成的例外物件②手動的生成一個例外物件,并拋出(throw)
程序二:"抓":可以理解為例外的處理方式:① try-catch-finally ② throws
2.例外處理方式一:try-catch-finally
try{
//可能出現例外的代碼
}catch(例外型別1 變數名1){
//處理例外的方式1
}catch(例外型別2 變數名2){
//處理例外的方式2
}catch(例外型別3 變數名3){
//處理例外的方式3
}
....
finally{
//一定會執行的代碼
}
說明:
-
finally是可選的,
-
使用try將可能出現例外代碼包裝起來,在執行程序中,一旦出現例外,就會生成一個對應例外類的物件,根據此物件的型別,去catch中進行匹配,
-
一旦try中的例外物件匹配到某一個catch時,就進入catch中進行例外的處理,一旦處理完成,就跳出當前的try-catch結構(在沒寫finally的情況,繼續執行其后的代碼
-
catch中的例外型別如果沒子父類關系,則誰宣告在上,誰宣告在下無所謂,catch中的例外型別如果滿足子父類關系,則要求子類一定宣告在父類的上面,否則,報錯,
-
常用的例外物件處理的方式: ① String getMessage() ② printStackTrace()
-
在try結構中宣告的變數,再出了try結構以后,就不能再被呼叫,
-
try-catch-finally結構可以嵌套,
總結:如何看待代碼中的編譯時例外和運行時例外?
體會1:使用try-catch-finally處理編譯時例外,是得程式在編譯時就不再報錯,但是運行時仍可能報錯,相當于我們使用try-catch-finally將一個編譯時可能出現的例外,延遲到運行時出現,
體會2:開發中,由于運行時例外比較常見,所以我們通常就不針對運行時例外撰寫try-catch-finally了,針對于編譯時例外,我們說一定要考慮例外的處理,
finally的再說明
①finally是可寫,可不寫的,
②finally中宣告的是一定會被執行的代碼,即使catch中又出現例外了,try中return陳述句,catch中return陳述句等情況,
③像資料庫連接、輸入輸出流、網路編程Socket等資源,JVM是不能自動的回收的,我們需要自己手動的進行資源的釋放,此時的資源釋放,就需要宣告在finally中,
面試題
- final、finally、finalize三者的區別?
類似:
- throw 和 throws
- Collection 和 Collections
- String 、StringBuffer、StringBuilder
- ArrayList 、 LinkedList
- HashMap 、LinkedHashMap
重寫、多載
結構不相似的:
抽象類、介面
== 、 equals()
sleep()、wait()
3.例外處理方式二
"throws + 例外型別"寫在方法的宣告處,指明此方法執行時,可能會拋出的例外型別,
一旦當方法體執行時,出現例外,仍會在例外代碼處生成一個例外類的物件,此物件滿足throws后例外型別時,就會被拋出,例外代碼后續的代碼,就不再執行!
4.對比兩種處理方式
try-catch-finally:真正的將例外給處理掉了,
throws:的方式只是將例外拋給了方法的呼叫者,并沒真正將例外處理掉,
體會開發中應該如何選擇兩種處理方式?
-
如果父類中被重寫的方法沒throws方式處理例外,則子類重寫的方法也不能使用throws,意味著如果子類重寫的方法中例外,必須使用try-catch-finally方式處理,
-
執行的方法a中,先后又呼叫了另外的幾個方法,這幾個方法是遞進關系執行的,我們建議這幾個方法使用throws的方式進行處理,而執行的方法a可以考慮使用try-catch-finally方式進行處理,
補充:
方法重寫的規則之一:
子類重寫的方法拋出的例外型別不大于父類被重寫的方法拋出的例外型別,
5.手動拋出例外物件
在程式執行中,除了自動拋出例外物件的情況之外,我們還可以手動的throw一個例外類的物件,
面試題
throw 和 throws區別:
throw 表示拋出一個例外類的物件,生成例外物件的程序,宣告在方法體內,
throws 屬于例外處理的一種方式,宣告在方法的宣告處
典型例題
class Student{
private int id;
public void regist(int id) throws Exception {
if(id > 0){
this.id = id;
}else{
//手動拋出例外物件
// throw new RuntimeException("您輸入的資料非法!");
// throw new Exception("您輸入的資料非法!");
throw new MyException("不能輸入負數");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/431983.html
標籤:其他
上一篇:Java中Hashmap的使用
下一篇:箱圖在資料預處理中的應用
