public class tryDemo { public static int show() { try { return 1; } finally { System.out.println("執行finally模組"); } } public static void main(String args[]) { System.out.println(show()); } }輸出結果如下:
執行finally模組
1
public class tryDemo { public static int show() { try { int a = 8 / 0; return 1; } catch (Exception e) { return 2; } finally { System.out.println("執行finally模組"); } } public static void main(String args[]) { System.out.println(show()); } }輸出結果為:
執行finally模組
2
public class tryDemo { public static int show() { try { int a = 8 / 0; return 1; } catch (Exception e) { return 2; } finally { System.out.println("執行finally模組"); return 0; } } public static void main(String args[]) { System.out.println(show()); } }輸出結果如下:
執行finally模組
0
public class tryDemo { public static int show() { int result = 0; try { return result; } finally { System.out.println("執行finally模組"); result = 1; } } public static void main(String args[]) { System.out.println(show()); } }輸出結果為:
執行finally模組
0
public class tryDemo { public static Object show() { Object obj = new Object(); try { return obj; } finally { System.out.println("執行finally模組"); obj = null; } } public static void main(String args[]) { System.out.println(show()); } }輸出結果為:
執行finally模組
[email protected]