淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)
網(wǎng)上有很多人探討Java中異常捕獲機(jī)制try...catch...finally塊中的finally語句是不是一定會被執(zhí)行?很多人都說不是,當(dāng)然他們的回答是正確的,經(jīng)過我試驗,至少有兩種情況下finally語句是不會被執(zhí)行的:
(1)try語句沒有被執(zhí)行到,如在try語句之前就返回了,這樣finally語句就不會執(zhí)行,這也說明了finally語句被執(zhí)行的必要而非充分條件是:相應(yīng)的try語句一定被執(zhí)行到。
(2)在try塊中有System.exit(0);這樣的語句,System.exit(0);是終止Java虛擬機(jī)JVM的,連JVM都停止了,所有都結(jié)束了,當(dāng)然finally語句也不會被執(zhí)行到。
當(dāng)然還有很多人探討Finally語句的執(zhí)行與return的關(guān)系,頗為讓人迷惑,不知道finally語句是在try的return之前執(zhí)行還是之后執(zhí)行?我也是一頭霧水,我覺得他們的說法都不正確,我覺得應(yīng)該是:finally語句是在try的return語句執(zhí)行之后,return返回之前執(zhí)行。這樣的說法有點(diǎn)矛盾,也許是我表述不太清楚,下面我給出自己試驗的一些結(jié)果和示例進(jìn)行佐證,有什么問題歡迎大家提出來。
1. finally語句在return語句執(zhí)行之后return返回之前執(zhí)行的。
public class FinallyTest1 { public static void main(String[] args) { System.out.println(test1()); } public static int test1() { int b = 20; try { System.out.println("try block"); return b += 80; } catch (Exception e) { System.out.println("catch block"); } finally { System.out.println("finally block"); if (b > 25) { System.out.println("b>25, b = " + b); } } return b; } }
運(yùn)行結(jié)果是:
try block finally block b>25, b = 100 100
說明return語句已經(jīng)執(zhí)行了再去執(zhí)行finally語句,不過并沒有直接返回,而是等finally語句執(zhí)行完了再返回結(jié)果。
如果覺得這個例子還不足以說明這個情況的話,下面再加個例子加強(qiáng)證明結(jié)論:
public class FinallyTest1 { public static void main(String[] args) { System.out.println(test11()); } public static String test11() { try { System.out.println("try block"); return test12(); } finally { System.out.println("finally block"); } } public static String test12() { System.out.println("return statement"); return "after return"; } }
運(yùn)行結(jié)果為:
try block return statement finally block after return
說明try中的return語句先執(zhí)行了但并沒有立即返回,等到finally執(zhí)行結(jié)束后再
這里大家可能會想:如果finally里也有return語句,那么是不是就直接返回了,try中的return就不能返回了?看下面。
2. finally塊中的return語句會覆蓋try塊中的return返回。
public class FinallyTest2 { public static void main(String[] args) { System.out.println(test2()); } public static int test2() { int b = 20; try { System.out.println("try block"); return b += 80; } catch (Exception e) { System.out.println("catch block"); } finally { System.out.println("finally block"); if (b > 25) { System.out.println("b>25, b = " + b); } return 200; } // return b; } }
運(yùn)行結(jié)果是:
try block finally block b>25, b = 100 200
這說明finally里的return直接返回了,就不管try中是否還有返回語句,這里還有個小細(xì)節(jié)需要注意,finally里加上return過后,finally外面的return b就變成不可到達(dá)語句了,也就是永遠(yuǎn)不能被執(zhí)行到,所以需要注釋掉否則編譯器報錯。
這里大家可能又想:如果finally里沒有return語句,但修改了b的值,那么try中return返回的是修改后的值還是原值?看下面。
3. 如果finally語句中沒有return語句覆蓋返回值,那么原來的返回值可能因為finally里的修改而改變也可能不變。
測試用例1:
public class FinallyTest3 { public static void main(String[] args) { System.out.println(test3()); } public static int test3() { int b = 20; try { System.out.println("try block"); return b += 80; } catch (Exception e) { System.out.println("catch block"); } finally { System.out.println("finally block"); if (b > 25) { System.out.println("b>25, b = " + b); } b = 150; } return 2000; } }
運(yùn)行結(jié)果是:
try block finally block b>25, b = 100 100
測試用例2:
import java.util.*; public class FinallyTest6 { public static void main(String[] args) { System.out.println(getMap().get("KEY").toString()); } public static Map<String, String> getMap() { Map<String, String> map = new HashMap<String, String>(); map.put("KEY", "INIT"); try { map.put("KEY", "TRY"); return map; } catch (Exception e) { map.put("KEY", "CATCH"); } finally { map.put("KEY", "FINALLY"); map = null; } return map; } }
運(yùn)行結(jié)果是:
FINALLY
為什么測試用例1中finally里的b = 150;并沒有起到作用而測試用例2中finally的map.put("KEY", "FINALLY");起了作用而map = null;卻沒起作用呢?這就是Java到底是傳值還是傳址的問題了,具體請看精選30道Java筆試題解答,里面有詳細(xì)的解答,簡單來說就是:Java中只有傳值沒有傳址,這也是為什么map = null這句不起作用。這同時也說明了返回語句是try中的return語句而不是 finally外面的return b;這句,不相信的話可以試下,將return b;改為return 294,對原來的結(jié)果沒有一點(diǎn)影響。
這里大家可能又要想:是不是每次返回的一定是try中的return語句呢?那么finally外的return b不是一點(diǎn)作用沒嗎?請看下面。
4. try塊里的return語句在異常的情況下不會被執(zhí)行,這樣具體返回哪個看情況。
public class FinallyTest4 { public static void main(String[] args) { System.out.println(test4()); } public static int test4() { int b = 20; try { System.out.println("try block"); b = b / 0; return b += 80; } catch (Exception e) { b += 15; System.out.println("catch block"); } finally { System.out.println("finally block"); if (b > 25) { System.out.println("b>25, b = " + b); } b += 50; } return 204; } }
運(yùn)行結(jié)果是:
try block catch block finally block b>25, b = 35 85
這里因 為在return之前發(fā)生了除0異常,所以try中的return不會被執(zhí)行到,而是接著執(zhí)行捕獲異常的catch 語句和最終的finally語句,此時兩者對b的修改都影響了最終的返回值,這時return b;就起到作用了。當(dāng)然如果你這里將return b改為return 300什么的,最后返回的就是300,這毋庸置疑。
這里大家可能又有疑問:如果catch中有return語句呢?當(dāng)然只有在異常的情況下才有可能會執(zhí)行,那么是在finally之前就返回嗎?看下面。
5. 當(dāng)發(fā)生異常后,catch中的return執(zhí)行情況與未發(fā)生異常時try中return的執(zhí)行情況完全一樣。
public class FinallyTest5 { public static void main(String[] args) { System.out.println(test5()); } public static int test5() { int b = 20; try { System.out.println("try block"); b = b /0; return b += 80; } catch (Exception e) { System.out.println("catch block"); return b += 15; } finally { System.out.println("finally block"); if (b > 25) { System.out.println("b>25, b = " + b); } b += 50; } //return b; } }
運(yùn)行結(jié)果如下:
try block catch block finally block b>25, b = 35 35
說明了發(fā)生異常后,catch中的return語句先執(zhí)行,確定了返回值后再去執(zhí)行finally塊,執(zhí)行完了catch再返回,finally里對b的改變對返回值無影響,原因同前面一樣,也就是說情況與try中的return語句執(zhí)行完全一樣。
最后總結(jié):finally塊的語句在try或catch中的return語句執(zhí)行之后返回之前執(zhí)行且finally里的修改語句可能影響也可能不影響try或catch中 return已經(jīng)確定的返回值,若finally里也有return語句則覆蓋try或catch中的return語句直接返回。
以上這篇淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis動態(tài)<if>標(biāo)簽使用避坑指南
這篇文章主要為大家介紹了MyBatis動態(tài)<if>標(biāo)簽使用避坑指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Druid(新版starter)在SpringBoot下的使用教程
Druid是Java語言中最好的數(shù)據(jù)庫連接池,Druid能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能,DruidDataSource支持的數(shù)據(jù)庫,這篇文章主要介紹了Druid(新版starter)在SpringBoot下的使用,需要的朋友可以參考下2023-05-05Java實(shí)現(xiàn)撲克牌的創(chuàng)建以及發(fā)放
在java當(dāng)中生成一副牌有很多種方法,有簡單易于理解的面向過程編程,也有面向?qū)ο竽K化編程,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)撲克牌的創(chuàng)建以及發(fā)放的相關(guān)資料,需要的朋友可以參考下2023-03-03Java使用MulticastSocket實(shí)現(xiàn)群聊應(yīng)用程序
這篇文章主要為大家詳細(xì)介紹了Java使用MulticastSocket實(shí)現(xiàn)群聊應(yīng)用程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05windows定時器配置執(zhí)行java jar文件的方法詳解
這篇文章主要給大家介紹了關(guān)于windows定時器配置執(zhí)行java jar文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11spring boot整合jsp及設(shè)置啟動頁面的方法
這篇文章主要給大家介紹了關(guān)于spring boot整合jsp及設(shè)置啟動頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09