深入剖析Java中的各種異常處理方式
1. 調(diào)試追蹤代碼:
public static void enterTryMethod() {
System.out.println("enter after try field");
}
public static void enterExceptionMethod() {
System.out.println("enter catch field");
}
public static void enterFinallyMethod() {
System.out.println("enter finally method");
}
2. 拋出Exception,沒(méi)有finally,當(dāng)catch遇上return
public static int catchTest() {
int res = 0;
try {
res = 10 / 0; // 拋出Exception,后續(xù)處理被拒絕
enterTryMethod();
return res; // Exception已經(jīng)拋出,沒(méi)有獲得被執(zhí)行的機(jī)會(huì)
} catch (Exception e) {
enterExceptionMethod();
return 1; // Exception拋出,獲得了調(diào)用方法并返回方法值的機(jī)會(huì)
}
}
后臺(tái)輸出結(jié)果:
enter catch field 1
3. 拋出Exception,當(dāng)catch體里有return,finally體的代碼塊將在catch執(zhí)行return之前被執(zhí)行
public static int catchTest() {
int res = 0;
try {
res = 10 / 0; // 拋出Exception,后續(xù)處理被拒絕
enterTryMethod();
return res; // Exception已經(jīng)拋出,沒(méi)有獲得被執(zhí)行的機(jī)會(huì)
} catch (Exception e) {
enterExceptionMethod();
return 1; // Exception拋出,獲得了調(diào)用方法并返回方法值的機(jī)會(huì)
} finally {
enterFinallyMethod(); // Exception拋出,finally代碼將在catch執(zhí)行return之前被執(zhí)行
}
}
后臺(tái)輸出結(jié)果:
enter catch field enter finally method 1
4. 不拋出Exception,當(dāng)finally代碼塊里面遇上return,finally執(zhí)行完后將結(jié)束整個(gè)方法
public static int catchTest() {
int res = 0;
try {
res = 10 / 2; // 不拋出Exception
enterTryMethod();
return res; // 獲得被執(zhí)行的機(jī)會(huì),但執(zhí)行需要在finally執(zhí)行完成之后才能被執(zhí)行
} catch (Exception e) {
enterExceptionMethod();
return 1;
} finally {
enterFinallyMethod();
return 1000; // finally中含有return語(yǔ)句,這個(gè)return將結(jié)束這個(gè)方法,不會(huì)在執(zhí)行完之后再跳回try或者catch繼續(xù)執(zhí)行,方法到此結(jié)束
}
}
后臺(tái)輸出結(jié)果:
enter after try field enter finally method 1000
5. 不拋Exception,當(dāng)finally代碼塊里面遇上System.exit()方法將結(jié)束和終止整個(gè)程序,而不只是方法
public static int catchTest() {
int res = 0;
try {
res = 10 / 2; // 不拋出Exception
enterTryMethod();
return res; // 獲得被執(zhí)行的機(jī)會(huì),但由于finally已經(jīng)終止程序,返回值沒(méi)有機(jī)會(huì)被返回
} catch (Exception e) {
enterExceptionMethod();
return 1;
} finally {
enterFinallyMethod();
System.exit(0); // finally中含有System.exit()語(yǔ)句,System.exit()將退出整個(gè)程序,程序?qū)⒈唤K止
}
}
后臺(tái)輸出結(jié)果:
enter after try field enter finally method
6. 拋出Exception,當(dāng)catch和finally同時(shí)遇上return,catch的return返回值將不會(huì)被返回,finally的return語(yǔ)句將結(jié)束整個(gè)方法并返回
public static int catchTest() {
int res = 0;
try {
res = 10 / 0; // 拋出Exception,后續(xù)處理將被拒絕
enterTryMethod();
return res; // Exception已經(jīng)拋出,沒(méi)有獲得被執(zhí)行的機(jī)會(huì)
} catch (Exception e) {
enterExceptionMethod();
return 1; // Exception已經(jīng)拋出,獲得被執(zhí)行的機(jī)會(huì),但返回操作將被finally截?cái)?
} finally {
enterFinallyMethod();
return 10; // return將結(jié)束整個(gè)方法,返回值為10
}
}
后臺(tái)輸出結(jié)果:
enter catch field enter finally method 10
7. 不拋出Exception,當(dāng)finally遇上return,try的return返回值將不會(huì)被返回,finally的return語(yǔ)句將結(jié)束整個(gè)方法并返回
public static int catchTest() {
int res = 0;
try {
res = 10 / 2; // 不拋出Exception
enterTryMethod();
return res; // 獲得執(zhí)行機(jī)會(huì),但返回將被finally截?cái)?
} catch (Exception e) {
enterExceptionMethod();
return 1;
} finally {
enterFinallyMethod();
return 10; // return將結(jié)束整個(gè)方法,返回值為10
}
}
后臺(tái)輸出結(jié)果:
enter after try field enter finally method 10
結(jié)論
Java的異常處理中,程序執(zhí)行完try里面的代碼塊之后,該方法并不會(huì)立即結(jié)束,而是繼續(xù)試圖去尋找該方法有沒(méi)有finally的代碼塊
如果沒(méi)有finally代碼塊,整個(gè)方法在執(zhí)行完try代碼塊后返回相應(yīng)的值來(lái)結(jié)束整個(gè)方法
如果有finally代碼塊,此時(shí)程序執(zhí)行到try代碼塊里的return一句之時(shí)并不會(huì)立即執(zhí)行return,而是先去執(zhí)行finally代碼塊里的代碼
若finally代碼塊里沒(méi)有return或沒(méi)有能夠終止程序的代碼,程序在執(zhí)行完finally代碼塊代碼之后再返回try代碼塊執(zhí)行return語(yǔ)句來(lái)結(jié)束整個(gè)方法。若 finally 代碼塊里有 return 或含有能夠終止程序的代碼,方法將在執(zhí)行完 finally 之后被結(jié)束,不再跳回 try 代碼塊執(zhí)行 return
在拋出異常的情況下,原理也是和上面的一樣的,你把上面說(shuō)到的 try 換成 catch 去理解就OK了。
相關(guān)文章
SpringSecurity+Mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶(hù)安全登錄認(rèn)證的實(shí)踐
Spring Security 是一個(gè)提供身份認(rèn)證、授權(quán)和防范常見(jiàn)攻擊的安全權(quán)限框架,本文主要介紹了SpringSecurity+Mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶(hù)安全登錄認(rèn)證的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)KFC點(diǎn)餐系統(tǒng),模擬肯德基快餐店的收銀系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
java數(shù)據(jù)庫(kù)開(kāi)發(fā)之JDBC基礎(chǔ)使用方法及實(shí)例詳解
這篇文章主要介紹了java數(shù)據(jù)庫(kù)開(kāi)發(fā)之JDBC基礎(chǔ)知識(shí)詳解,需要的朋友可以參考下2020-02-02
淺談Java高并發(fā)解決方案以及高負(fù)載優(yōu)化方法
這篇文章主要介紹了淺談Java高并發(fā)解決方案以及高負(fù)載優(yōu)化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot整合Mybatis-plus的具體過(guò)程使用
這篇文章主要介紹了SpringBoot?整合mybatis+mybatis-plus的步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Java 對(duì)接飛書(shū)多維表格使用詳解(微服務(wù))
本文詳細(xì)介紹了如何基于飛書(shū)開(kāi)放平臺(tái)在微服務(wù)項(xiàng)目中操作飛書(shū)多維表格,包括應(yīng)用創(chuàng)建、授權(quán)、多維表數(shù)據(jù)操作(新增、查詢(xún)、刪除)以及Java SDK實(shí)現(xiàn)等步驟,感興趣的朋友跟隨小編一起看看吧2024-12-12
java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例
本文主要介紹了java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06

