Java異常機制、Java異常的捕獲順序、Throw和Throws的區別、異常中的return

綜合網上各類博客總結出來,只供學習。

Java異常的捕獲順序

         package com.view.exception;

 

public class TestMoreCatch {

   public static void main(String[] args) {

      int a = 6;

      int b = 0;

      try { // try監控區域

        if (b == 0)

           throw new ArithmeticException(); // 通過throw語句拋出異常

        System.out.println("a/b的值是:" + a / b);

      } catch (ArithmeticException e) { // catch捕捉異常

        System.out.println("程序出現異常,變量b不能爲0");//運行

      } catch (Exception e) {

        /**

         * 範圍更大的Exception不但必須放在後面

         * 並且放在後面還不會被運行(被前面的範圍更小的

         * 異常攔截了)

         */

        System.out.println("執行exception 父類的異常");

      }

      System.out.println("程序正常結束。");

   }

}

 

/*

 * 要點1:儘管ArithmeticException繼承自Exception。可是當發生ArithmeticException異常

 * 並捕獲的時候,就僅僅會捕獲實際發生的這個異常。並不會由於Exception是其父類而

 *

 * 運行Exception那個catch子句。

 *

 * 要點2:可是假設你嘗試將範圍更大的Exceptioncatch語句放到的catch語句的前面,那麼就會發生

 *

 * catch子句不可到達的錯誤“Unreachablecatch block for ArithmeticException.

 *

 * Itis already handled by the catch block for Exception”

 *

 * 即範圍更大的異常(父類)必須放在後面,假設沒有繼承關係,比方ClassNotFoundException

 *

 * ArithmeticExceptioncatch子句之間就無所謂先後關係。

 */

Throw和Throws的區別:

 

 

Throw

作用在方法內,表示拋出具體異常,由方法體內的語句處理。

具體向外拋出的動作,所以它拋出的是一個異常實體類。若執行了Throw一定是拋出了某種異常。

Throws

作用在方法的聲明上,表示如果拋出異常,則由該方法的調用者來進行異常處理。

主要的聲明這個方法會拋出會拋出某種類型的異常,讓它的使用者知道捕獲異常的類型。

出現異常是一種可能性,但不一定會發生異常。

實例:

 

void testException(int a) throws IOException,{

           try{

                 ......

           }catch(Exception1 e){

              throw e;

           }catch(Exception2 e){

              System.out.println("出錯了!");

           }

           if(a!=b)

              throw new  Exception3("自定義異常");

}

異常兩種處理方式

  1. throws        拋出異常,自己不處理
  2. try-catch     自己處理異常

拋出異常

誰調用拋給誰去解決

分成兩步:

  1. 拋異常      throw new Exception(“”)
  2. 接異常      throws Exception

異常中的return

public class TestReturn {

   public static void main(String[] args) {

        System.out.println("=============NoException==================");//1

        System.out.println(NoException());//2

        System.out.println("===============================");   //3

   }

   public static int NoException(){

        int i=10;

        try{

          System.out.println("i in try block is"+i);//2.1

          return --i; //2.2 i=9

        }

        catch(Exception e){

          --i;

          System.out.println("i in catch - form try block is"+i);

          return --i;

        }

        finally{    

          System.out.println("i in finally - from try or catch block is"+i);//2.3

          return --i;//2.4 i=8

        } 

   }

}

1.try塊中沒有拋出異常,trycatchfinally塊中都有return語句

執行順序:
執行try塊,執行到return語句時,先執行return的語句,--i,但是不返回到main方法,執行finally塊,遇到finally塊中的return語句,執行--i,並將值返回到main方法,這裏就不會再回去返回try塊中計算得到的值。
結論:try-catch-finally都有return語句時,沒有異常時,返回值是finally中的return返回的

 


2.try塊中沒有拋出異常,僅trycatch中有return語句

執行順序:
try中執行完return的語句後,不返回,執行finally塊,finally塊執行結束後,返回到try塊中,返回i在try塊中最後的值。
結論:try-catch都有return語句時,沒有異常時,返回值是try中的return返回的。

 

public class TestReturn02 {

   public static void main(String[] args) {

      System.out.println("=============NoException==================");// 1

      System.out.println(HaveException());// 2

      System.out.println("==============================="); // 3

   }

 

   public static int HaveException() {

      int i = 10;

      try {

         System.out.println("i in try block is" + i);// 2.1

         i = i / 0;

         return --i;

      } catch (Exception e) {

         System.out.println("i in catch - form try block is" + i);// 2.2

         --i;

         System.out.println("i in catch block is" + i);// 2.3

         return --i;// 2.4

      } finally {

         System.out.println("i in finally - from try or catch block is--" + i);// 2.5

         --i;

         System.out.println("i in finally block is--" + i);// 2.6

         return --i;// 2.7

      }

   }

}


3.try塊中拋出異常,trycatchfinally中都有return語句
 

執行順序:
拋出異常後,執行catch塊,在catch塊的return的--i執行完後,並不直接返回而是執行finally,因finally中有return語句,所以,執行,返回結果6。
結論:try塊中拋出異常,try、catch和finally中都有return語句,返回值是finally中的return。

4.try塊中拋出異常,trycatch中都有return語句

執行順序:
拋出異常後,執行catch塊,執行完finally語句後,依舊返回catch中的執行return語句後的值,而不是finally中修改的值。
結論:返回的catchreturn值。

public class TestReturn03 {

   public static void main(String[] args) {

      System.out.println("=============NoException==================");// 1

      System.out.println(HaveException03());// 2

      System.out.println("==============================="); // 3

   }

 

   public static int HaveException03() {

      int i = 10;

      try {

         System.out.println("i in try block is" + i);//2.1

         i = i / 0;

         return --i;

      } catch (Exception e) {

         System.out.println("i in catch - form try block is" + i);//2.2

         int j = i / 0;

         return --i;

      } finally {

         System.out.println("i in finally - from try or catch block is" + i);//2.3

         --i//2.4

         System.out.println("i in finally block is" + i);//2.5

         return --i;     //2.6

      }

   }

}

5.trycatch中都出現異常,在finally中有返回

執行順序:
try塊中出現異常到catchcatch中出現異常到finallyfinally中執行到return語句返回,不檢查異常。
結論:返回finallyreturn值。

 

6.只在函數最後出現return語句

結論:返回函數最後的return語句

總體結論:
結論一:
return語句並不是函數的最終出口,如果有finally語句,這在return之後還會執行finally(return的值會暫存在棧裏面,等待finally執行後再返回)
結論二:
finally裏面不建議放return語句,根據需要,return語句可以放在try和catch裏面和函數的最後。可行的做法有四:
(1)return語句只在函數最後出現一次。
(2)return語句僅在try和catch裏面都出現。
(3)return語句僅在try和函數的最後都出現。
(4)return語句僅在catch和函數的最後都出現。
注意,除此之外的其他做法都是不可行的,編譯器會報錯。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章