標籤:method ret void java基礎 alt src lin cat images
//摘自其他人的部落格 // catch 後續處理工作
3 public static boolean catchMethod() {
4 System.out.print("call catchMethod and return --->> ");
5 return false;
6 }
7 // finally後續處理工作
8 public static void finallyMethod() {
9 System.out.println();
10 System.out.print("call finallyMethod and do something --->> ");
11 }
12
1. 拋出 Exception,沒有 finally,當 catch 遇上 return
1
2public static boolean catchTest() {
3 try {
4 int i = 10 / 0; // 拋出 Exception,後續處理被拒絕
5 System.out.println("i vaule is : " + i);
6 return true; // Exception 已經拋出,沒有獲得被執行的機會
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod(); // Exception 拋出,獲得了調用方法並返回方法值的機會
10 }
11 }
12
後台輸出結果:
1
2 -- Exception --
3call catchMethod and return --->> false
4
2. 拋出 Exception,當 catch 體裡有 return,finally 體的代碼塊將在 catch 執行 return 之前被執行
1
2public static boolean catchFinallyTest1() {
3 try {
4 int i = 10 / 0; // 拋出 Exception,後續處理被拒絕
5 System.out.println("i vaule is : " + i);
6 return true; // Exception 已經拋出,沒有獲得被執行的機會
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod(); // Exception 拋出,獲得了調用方法的機會,但方法值在 finally 執行完後才返回
10 }finally{
11 finallyMethod(); // Exception 拋出,finally 代碼塊將在 catch 執行 return 之前被執行
12 }
13 }
14
後台輸出結果:
1
2 -- Exception --
3call catchMethod and return --->>
4call finallyMethod and do something --->> false
5
3. 不拋 Exception,當 finally 代碼塊裡面遇上 return,finally 執行完後將結束整個方法
1
2public static boolean catchFinallyTest2() {
3 try {
4 int i = 10 / 2; // 不拋出 Exception
5 System.out.println("i vaule is : " + i);
6 return true; // 獲得被執行的機會,但執行需要在 finally 執行完成之後才能被執行
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod();
10 }finally{
11 finallyMethod();
12 return false; // finally 中含有 return 語句,這個 return 將結束這個方法,不會在執行完之後再跳回 try 或 catch 繼續執行,方法到此結束,返回 false
13 }
14 }
15
後台輸出結果:
1
2i vaule is : 5
3
4call finallyMethod and do something --->> false
5
4. 不拋 Exception,當 finally 代碼塊裡面遇上 System.exit() 方法 將結束和終止整個程式,而不只是方法
1
2public static boolean finallyExitTest() {
3 try {
4 int i = 10 / 2; // 不拋出 Exception
5 System.out.println("i vaule is : " + i);
6 return true; // 獲得被執行的機會,但由於 finally 已經終止程式,傳回值沒有機會被返回
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true;
10 }finally {
11 finallyMethod();
12 System.exit(0);// finally 中含有 System.exit() 語句,System.exit() 將退出整個程式,程式將被終止
13 }
14 }
15
後台輸出結果:
1
2i vaule is : 5
3
4call finallyMethod and do something --->>
5
5. 拋出 Exception,當 catch 和 finally 同時遇上 return,catch 的 return 傳回值將不會被返回,finally 的 return 語句將結束整個方法並返回
1
2public static boolean finallyTest1() {
3 try {
4 int i = 10 / 0; // 拋出 Exception,後續處理被拒絕
5 System.out.println("i vaule is : " + i);
6 return true; // Exception 已經拋出,沒有獲得被執行的機會
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true; // Exception 已經拋出,獲得被執行的機會,但返回操作將被 finally 截斷
10 }finally {
11 finallyMethod();
12 return false; // return 將結束整個方法,返回 false
13 }
14 }
15
後台輸出結果:
1
2 -- Exception --
3
4call finallyMethod and do something --->> false
5
6. 不拋出 Exception,當 finally 遇上 return,try 的 return 傳回值將不會被返回,finally 的 return 語句將結束整個方法並返回
1
2public static boolean finallyTest2() {
3 try {
4 int i = 10 / 2; // 不拋出 Exception
5 System.out.println("i vaule is : " + i);
6 return true; // 獲得被執行的機會,但返回將被 finally 截斷
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true;
10 }finally {
11 finallyMethod();
12 return false; // return 將結束這個方法,不會在執行完之後再跳回 try 或 catch 繼續執行,返回 false
13 }
14 }
15
後台輸出結果:
1
2i vaule is : 5
3
4call finallyMethod and do something --->> false
5
Java基礎--try-Catch-finally