一、異常體系結(jié)構(gòu)
1.什么是異常
在java程序運(yùn)行過程中,發(fā)生了一些意料之外的情況就是異常。在java中異常一顆分為兩大類:
(錯(cuò)誤)Error 和 (異常)Exception。
- 對于(錯(cuò)誤)Error來說,我們無法通過程序去解決所有的錯(cuò)誤,僅僅可以去嘗試捕獲這些錯(cuò)誤,但是無法處理,往往錯(cuò)誤的發(fā)生對程序來說是重大的致命性問題,需要通過較大的調(diào)整去修正它。
- 對于(異常)Exception來說,它是我們可以通過程序去處理的,我們可以對這些異常情況做出捕獲和處理。
2.異常的體系結(jié)構(gòu)
? Throwable
? Error Exception
? RutimeException
編譯時(shí)異常
說明: RutimeException
和一些編譯時(shí)異常繼承了Exception
,Exception
和Error
繼承了Throwable
。
運(yùn)行時(shí)異常(RutimeException
)在代碼中我們往往不需要去捕獲它,而是通過處理代碼的邏輯來規(guī)避運(yùn)行時(shí)異常。常見的5種運(yùn)行時(shí)異常:
-
ClassCastException
(類轉(zhuǎn)換異常) -
ArrayIndexOutOfBoundsException
(數(shù)組越界異常) -
NullPointerException
(空指針) -
ArrayStoreException
(數(shù)據(jù)存儲(chǔ)異常,操作數(shù)組時(shí)類型不一致) -
NumberFormatException
數(shù)字格式化異常
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Demo{ public void test(){ //此代碼聲明這里會(huì)拋出一個(gè)運(yùn)行時(shí)異常 throw new RutimeException(); } } public class DemoTest{ public static void main(String[] args){ Demo demo = new Demo() demo.test(); } } |
從上述代碼種可以看出,運(yùn)行時(shí)異常產(chǎn)生時(shí),代碼可以正常的編譯與運(yùn)行,但是運(yùn)行結(jié)果會(huì)拋出一個(gè)運(yùn)行時(shí)異常。
說明:運(yùn)行時(shí)異常會(huì)向上傳播。
二、異常處理
當(dāng)異常發(fā)生時(shí),我們需要對其進(jìn)行處理,處理異常有兩種方式:
一種使用try…catch…finally進(jìn)行捕獲;
另一種使用throws的方式進(jìn)行拋出;
try…catch…finally
語法
1
2
3
4
5
6
7
|
try { // 可能發(fā)生異常的代碼塊 } catch ( /* 需要捕獲的異常類 */ ) { // 捕獲到異常后的處理代碼塊 } finally { // 不管是否會(huì)發(fā)生異常,此代碼塊都會(huì)被執(zhí)行 } |
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class Demo1{ public static void main(String[] args){ String[] strs = { "張三" , "李四" , "王五" }; try { System.out.println( "-------" ); System.out.println(strs[ 3 ]); // 此處會(huì)發(fā)生異常,當(dāng)異常發(fā)生后,后面的代碼都不會(huì)被執(zhí)行到 for (String str : strs) { System.out.println(str); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println( "++++++++++" ); } catch (Exception e) { System.out.println( "++++++++++" ); } finally { System.out.println( "--------------" ); } } } |
說明:在try…catch…語句中,try語句中產(chǎn)生異常的代碼后面的代碼不會(huì)被執(zhí)行,產(chǎn)生異常后直接到了catch語句塊中。catch語句塊可以寫多個(gè),catch語句分支是從上到下依次找對應(yīng)類型的,但是注意catch捕獲的異常類型中父類型必須寫在子類型的后面。
finally語句塊是無論異常是否產(chǎn)生都會(huì)執(zhí)行的。常用與資源的釋放。
throw 與 throws
- throws是異常處理機(jī)制的一種,而throw不是;
- throws是聲明異常類;而throw是拋出異常對象;
- throws可以聲明多個(gè)異常類,而throw只能拋出一個(gè)異常對象;
- throws是聲明在方法上的,而throw是定義在方法內(nèi)的;
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class Demo{ public void test() throws Exception{ //此代碼聲明這里會(huì)拋出一個(gè)異常 throw new Exception(); } } public class DemoTest{ public static void main(String[] args){ Demo demo = new Demo() try { demo.test(); } catch (Exception e) { System.out.println( "++++++++++" ); } finally { System.out.println( "--------------" ); } } } |
三、自定義異常
運(yùn)行時(shí)異常
繼承 RutimeException
示例:
1
2
3
4
5
|
public class MyRutimeException extends RutimeException { public MyRutimeException(String message){ super (message); } } |
編譯時(shí)異常
繼承Exception
示例:
1
2
3
4
5
|
public class MyException extends Exception { public MyException(String message){ super (message); } } |
總結(jié)
本篇文章就到這里了,希望對你有所幫助,也希望您能夠多多關(guān)注服務(wù)器之家的更多內(nèi)容!
原文鏈接:https://blog.csdn.net/ww741258963123/article/details/117670481