try、catch、retrun執行順序
摘要:
public class TryTest {
public static void main(String[] args) {
String result = get();
System.out.println(result);
...
public class TryTest { public static void main(String[] args) { String result = get(); System.out.println(result); } public static String get(){ int value = 0; try { System.out.println("try……"); //等式1/0 :分母為0 的明顯錯誤——製造錯誤(用於拋異常) int result = 1 / value; return "111"; } catch (Exception e) { System.out.println("catch……"); return "444"; } finally { System.out.println("finally……"); return "333"; } return "222"; }
執行結果: try…… catch…… finally…… 333
public class TryTest { public static void main(String[] args) { // 呼叫 測試方法 String result = get(); // 列印 測試方法返回的結果 System.out.println(result); } public static String get(){ try { System.out.println("try……"); return "111"; } catch (Exception e) { System.out.println("catch……"); } finally { System.out.println("finally……"); } return "222"; } }
執行結果: try…… finally…… 111

trycatch執行順序.png