try catch finally執行機制

2021-06-18 11:50:23 字數 384 閱讀 2335

finally的執行

如下的程式所示,注釋中是執行的順序

public class test   

public static string test() finally  

}  public static string test1()   }

finally其實是僅在return ; 語句執行前執行,如果return 乙個函式,那麼會先執行函式,但如果函式內有(return ;)語句,那麼finally就會在這個(return ;)語句前執行。除非呼叫system.exit()讓程式退出或斷電等因素致使程式中止,否則,無論任何因素,finally塊都一定會執行!!

try catch finally執行順序

public class test public static int ma catch exception e finally 說明 不出現異常情況 執行順序 try finally 出現異常情況 執行順序 try catch finally try中有返回語句,沒有異常 執行順序 try ret...

try catch finally執行順序

結論 1 不管有木有出現異常,finally塊中 都會執行 2 當try和catch中有return時,finally任會執行 3 finally是在return表示式運算後前執行的,所以函式返回值是在finally執行前確定的 4 finally中最好不要包含return,否則程式會提前退出,返回...

try catch finally使用體會

try catch finally public class finallytest static int test finally 結果是2。在try語句中,在執行return語句時,要返回的結果已經準備好了,就在此時,程式轉到finally執行了。在轉去之前,try中先把要返回的結果存放到不同於...