Java語言中finally是否一定會執(zhí)行你知道嗎
簡介
我們都知道,finally 作為異常處理的一部分,它只能緊跟在try/catch語句后,附帶一個語句塊,表示這段語句,“在正常情況下”,最終一定會被執(zhí)行(不管有沒有拋出異常),經(jīng)常被用在需要釋放資源的情況下。那么在我們的應(yīng)用在運行中,一定會運行finally代碼塊嗎?其實不是的,有以下幾種情況我們的finally代碼塊是不會運行的。
finally 代碼塊不會運行的情況
情況一:代碼流程并未進(jìn)入try語句塊
這也是最好理解的情況,如果代碼流程不進(jìn)入try代碼塊,則相應(yīng)的catch和finally代碼塊自然不會執(zhí)行。
public static void main(String[] args) { int i = 0; System.out.println("enter main block"); boolean flag = false; if (flag) { try { System.out.println("enter try block"); i = i / i; } catch (Exception e) { System.out.println("enter catch block"); }finally { System.out.println("enter finally block"); } } }
運行結(jié)果為:
enter main block
情況二: 使用了 System.exit(int) 退出程序
在進(jìn)入try或catch塊后,使用了 System.exit(int) 退出程序。
public static void main(String[] args) { int i = 0; System.out.println("enter main block"); try { System.out.println("enter try block"); System.exit(0); i = i / i; } catch (Exception e) { System.out.println("enter catch block"); } finally { System.out.println("enter finally block"); } }
或
public static void main(String[] args) { int i = 0; System.out.println("enter main block"); try { System.out.println("enter try block"); i = i / i; } catch (Exception e) { System.exit(0); System.out.println("enter catch block"); } finally { System.out.println("enter finally block"); } }
運行結(jié)果為:
enter main block
enter try block
但是呢,如果 System.exit(int) 在try代碼塊異常語句之后, finally 還是會被執(zhí)行,因為已經(jīng)沒有機會執(zhí)行 System.exit(int) ,程序已經(jīng)退出了,比如:
public static void main(String[] args) { int i = 0; System.out.println("enter main block"); try { System.out.println("enter try block"); i = i / i; System.exit(0); } catch (Exception e) { System.out.println("enter catch block"); } finally { System.out.println("enter finally block"); } }
運行結(jié)果為:
enter main block
enter try block
enter catch block
enter finally block
情況三:程序所在的線程死亡
在當(dāng)前線程死亡的情況下,finally里的語句也不會執(zhí)行,比如干擾中斷,或者程序外部kill該線程,或者是意外中止。
public static void main(String[] args) { int i = 0; System.out.println("enter main block"); try { System.out.println("enter try block"); // 模擬執(zhí)行任務(wù)10s,然后在執(zhí)行任務(wù)過程中殺死該線程 Thread.sleep(10 * 1000); i = i / i; } catch (Exception e) { System.out.println("enter catch block"); } finally { System.out.println("enter finally block"); } }
這里在休眠里,用kill命令,殺死該線程,模擬非正常退出,最后運行結(jié)果為:
enter main block
enter try block
這里值得注意的是,我們常常在try語句塊里獲取了一些臨界資源,然后finally語句塊里釋放該資源。此時,如果正常獲得取資源后,程序非正常中斷,則我們并未正常釋放該資源,就會導(dǎo)致資源可能會被無限占用,所以這里要考慮一下其它的解決方法,比如給資源設(shè)置一個使用時間等,到期自動收回。
情況四:其它非正常退出
還有其它非正常退出(道理同上,就不演示了),也會導(dǎo)致finally代碼塊不執(zhí)行,比如物理關(guān)閉電源,關(guān)閉 CPU等。這些其實在開發(fā)生產(chǎn)環(huán)境中是常有出現(xiàn)的,比如在開發(fā)中,某一臺服務(wù)器獲取鎖后,不小心斷電或宕機了(未成功釋放鎖),然后導(dǎo)致別的機器也不能獲得到鎖(如果鎖無時間限制),最終導(dǎo)致出現(xiàn)系統(tǒng)型的問題。完事以后,你們開發(fā)也不知道發(fā)生了什么事,就把所有服務(wù)都重啟了一次,解決了問題,最后說一句:“??!還是重啟大法好”。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
SpringBoot?AOP統(tǒng)一處理Web請求日志的示例代碼
springboot有很多方法處理日志,例如攔截器,aop切面,service中代碼記錄等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot?AOP統(tǒng)一處理Web請求日志的相關(guān)資料,需要的朋友可以參考下2023-02-02Triple協(xié)議支持Java異常回傳設(shè)計實現(xiàn)詳解
這篇文章主要為大家介紹了Triple協(xié)議支持Java異?;貍髟O(shè)計實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12詳解Spring Cloud Finchley版中Consul多實例注冊的問題處理
這篇文章主要介紹了詳解Spring Cloud Finchley版中Consul多實例注冊的問題處理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Maven 搭建SpringMVC+Hibernate項目詳解
本文主要介紹Maven 搭建SpringMVC+Hibernate的知識,這里整理了詳細(xì)的資料,并附示例代碼,有興趣的小伙伴可以參考下2016-09-09Java Web學(xué)習(xí)教程之Hibernate And MyBatis的理解
這篇文章主要給大家介紹了關(guān)于Java Web學(xué)習(xí)教程之Hibernate And MyBatis的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04