Java基礎--try-Catch-finally

來源:互聯網
上載者:User

標籤: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

Java基礎--try-Catch-finally

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.