finally 一定會(huì)執(zhí)行(實(shí)例代碼)
如下所示:
class Exc{
int a;
int b;
}
public class Except {
@SuppressWarnings("finally")
static int compute (){
Exc e = new Exc();
e.a = 10;
e.b = 10;
int res = 0 ;
try{
res = e.a / e.b;
System.out.println("try ……");
return res + 1;
}catch(NullPointerException e1){
System.out.println("NullPointerException occured");
}catch(ArithmeticException e1){
System.out.println("ArithmeticException occured");
}catch(Exception e3){
System.out.println("Exception occured");
}finally{
System.out.println("finnaly occured");
}
System.out.println(res);
return res+3;
}
public static void main(String[] args){
int b = compute();
System.out.println("mian b= "+b);
}
}
輸出:
try …… finnaly occured mian b= 2
結(jié)論: 如果沒有異常, 則執(zhí)行try 中的代碼塊,直到 try 中的 return,接著執(zhí)行 finally 中的代碼塊,finally 執(zhí)行完后 , 回到try 中執(zhí)行 return 。退出函數(shù)。
class Exc{
int a;
int b;
}
public class Except {
@SuppressWarnings("finally")
static int compute (){
Exc e = new Exc();
// e.a = 10;
// e.b = 10;
int res = 0 ;
try{
res = e.a / e.b;
System.out.println("try ……");
return res + 1;
}catch(NullPointerException e1){
System.out.println("NullPointerException occured");
}catch(ArithmeticException e1){
System.out.println("ArithmeticException occured");
}catch(Exception e3){
System.out.println("Exception occured");
}finally{
System.out.println("finnaly occured");
}
System.out.println(res);
return res+3;
}
public static void main(String[] args){
int b = compute();
System.out.println("mian b= "+b);
}
}
輸出:
ArithmeticException occured finnaly occured 0 mian b= 3
結(jié)論: 如果try 中有異常, 則在異常語(yǔ)句處,跳轉(zhuǎn)到catch 捕獲的異常代碼塊, 執(zhí)行完 catch 后,再執(zhí)行 finally ,跳出 try{}catch{}finally{} ,繼續(xù)向下執(zhí)行,不會(huì)去執(zhí)行try中 后面的語(yǔ)句。
以上這篇finally 一定會(huì)執(zhí)行(實(shí)例代碼)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot前后端傳輸加密設(shè)計(jì)實(shí)現(xiàn)方案
這篇文章主要給大家介紹了關(guān)于SpringBoot前后端傳輸加密設(shè)計(jì)實(shí)現(xiàn)方案的相關(guān)資料,包括數(shù)據(jù)加密方案、解密傳輸數(shù)據(jù)實(shí)現(xiàn)方案和響應(yīng)數(shù)據(jù)加密實(shí)現(xiàn)方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
應(yīng)用啟動(dòng)數(shù)據(jù)初始化接口CommandLineRunner和Application詳解
這篇文章主要介紹了應(yīng)用啟動(dòng)數(shù)據(jù)初始化接口CommandLineRunner和Application詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
解決Idea報(bào)錯(cuò)There is not enough memory
在使用Idea開發(fā)過程中,可能會(huì)遇到因內(nèi)存不足導(dǎo)致的閃退問題,出現(xiàn)"There is not enough memory to perform the requested operation"錯(cuò)誤時(shí),可以通過調(diào)整Idea的虛擬機(jī)選項(xiàng)來(lái)解決,方法是在Idea的Help菜單中選擇Edit Custom VM Options2024-11-11
Spring Security實(shí)現(xiàn)禁止用戶重復(fù)登陸的配置原理
這篇文章主要介紹了Spring Security實(shí)現(xiàn)禁止用戶重復(fù)登陸的配置原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
SpringMVC事件監(jiān)聽ApplicationListener實(shí)例解析
這篇文章主要介紹了SpringMVC事件監(jiān)聽ApplicationListener實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能
這篇文章主要介紹了springboot實(shí)現(xiàn)添加郵件發(fā)送及壓縮功能 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07

