TryCatchFinally窮舉測試

2021-09-27 11:26:28 字數 1280 閱讀 7965

package test.liuwei;

import lombok.extern.slf4j.slf4j;

/** * @author liuwei

* @date 2019-09-27 09:45

* @desc trycatchfinally窮舉測試

* 演示在返回void以及非void時,尤其是在tcf組合中存在多個return語句時,tcf塊的執行順序

*/@slf4j

public class trycatchfinallytest

private static void voidtest(string str)catch (numberformatexception e)finally

log.info("始終執行外部**塊");

}private static string returntest4tcf(string str)catch (numberformatexception e)finally

}private static string returntest4cf(string str)catch (numberformatexception e)finally

}private static string returntest4tf(string str)catch (numberformatexception e)finally

}private static string returntest4tc(string str)catch (numberformatexception e)finally

}private static string returntest4t(string str)catch (numberformatexception e)finally

return "外部return,異常時返回";

}private static string returntest4c(string str)catch (numberformatexception e)finally

return "外部return,無異常時返回";

}private static string returntest4f(string str)catch (numberformatexception e)finally

}private static string returntest(string str)catch (numberformatexception e)finally

return "返回外部return";

}}

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中先把要返回的結果存放到不同於...