Java基礎(八),java基礎

來源:互聯網
上載者:User

Java基礎(八),java基礎
一、異常

  異常是指程式在運行過程產生的不正常情況。語法錯誤不算異常。

1、異常體系

  Throwable:Java語言中所有錯誤或異常的超類;Throwable 類的子類有兩個:1、Error;2、Exception。

1、所有的異常類都是 java.lang.Exception 的子類,Exception相對來說,可以控制,可以處理

2、Java 程式通常不捕獲錯誤。錯誤一般發生在嚴重故障時,它們在Java程式處理的範疇之外。例如:JVM 記憶體溢出。

3、Error 用來指示運行時環境發生的錯誤。

4、通過看子類是Exception結尾還是Error結尾,來判斷是Exception體系還是Error體系。

異常分類

運行時異常:RuntimeException 以及它子類的類型。

               如果沒有處理(拋出或者try-catch),編譯不會報錯

非運行時異常:Exception 以及它的子類,但是除去RuntimeException和它的子類。

               如果沒有處理(拋出或者try-catch),編譯的時候報錯

 2、異常處理

  異常處理有兩種方式:

1、自己處理try -catch

  寫在可能發生異常代碼的地方,如果出現異常,產生異常,就會被捕捉到,捕捉的異常資訊放到異常類型的變數裡面去;這樣就可以進行處理這個異常。

  如果沒有異常出現,就不捕捉,就往下面繼續執行走。

文法結構:

    try{    //可能出現異常    }catch(異常類型 變數名){    //處理異常代碼}

處理異常代碼:

1、把錯誤提示給使用者。直接調用 printStackTract方法就列印異常資訊。

2、把異常資訊,儲存到記錄檔,便於查看。

2、自己不處理,向外拋出

  拋出到JVM,還可能在實際開發過程,遇到方法調用方法,調用很多層,那種拋出,就像上一層調用者進行拋出。

throws 和 throw的區別:

throws 表示向外拋出異常,位置:方法的(){}之間,特點:後面跟多個異常。

throw 產生一個異常對象,位置:方法裡面。特點: 後面跟一個異常對象,相等於return的效果。

 1 public class ThrowsDemo { 2     /** 3      * 計算兩個整數相除 4      * @param a 5      * @param b 6      * @throws Exception 7      */ 8     public void div(int a, int b) throws Exception {  // 拋出異常 9         System.out.println(a / b);10     }11 12     /**13     * 測試div14     */15     @Test16     public void testDiv() throws Exception {17         div(1, 0);  //列印異常:java.lang.ArithmeticException: / by zero18     }19 }
3、多異常處理

語句結構:

1     try{2     //可能出現異常的代碼。。。3     }catch(異常類型 變數名){4     // 處理異常的代碼5     }catch(異常類型 變數名){6     // 處理異常的代碼7     }catch(異常類型 變數名){8     // 處理異常的代碼9  }
3、異常finally語句

除了上面兩種撲捉異常結構以外,還有其他的結構,可以在最後面添加一個finally

文法結構:

    try{    //可能出現異常的代碼    }catch(異常類型 e){     //處理異常:        //1 列印到控制台        //2 儲存到檔案        //3 還可能向外拋出新的異常    }finally{   }

finally單詞什麼含義,表示最終的意思。

1、關閉流資源或者釋放鎖--線程。

2、如果在finally前面沒有執行系統退出(system.exit(0))的語句,此處的代碼始終都會執行。

3、不要在此處放return語句來返回一個資料。

 1 public class ExceptionTest { 2     /** 3      * 測試出現異常時,程式的執行順序 4      */ 5     @Test 6     public void test1(){ 7         try { 8             System.out.println("------執行1------"); 9             System.out.println("------執行2------");10             System.out.println("------num------" + (1/0));  //出現異常之後,不執行後面代碼,即不列印------執行3------11             System.out.println("------執行3------");12         } catch ( ArithmeticException e) {13             System.out.println("------catch1------");14             System.out.println("------catch2------");15             e.printStackTrace();  //列印異常資訊 java.lang.ArithmeticException: / by zero16             System.out.println("------catch3------");17         } finally {  //不管是否有異常都要執行18             System.out.println("------finally1------");19             System.out.println("------finally2------");20             System.out.println("------finally3------");21         }22     }23 }
4、自訂異常

  建立了一個異常類,需要類比產生自訂異常的情境:

1、自訂一個異常類型,需要建立一個類繼承Exception或者RuntimeException,覆寫裡面的兩個構造方法

2、類比一種情境,建立一種自訂異常的對象,通過throw建立異常對象。

3、使用上面情境,拋出或者處理

 1、自訂一個異常類型。

 1 /** 2  * 自己定義異常對象 3  */ 4 public class LoginUserException extends RuntimeException{ 5     public LoginUserException() {  //覆寫構造方法 6         super(); 7     } 8  9     public LoginUserException(String message) {   //覆寫構造方法10         super(message);11     }12 } 

2、使用throw產生異常對象LoginUserException。

 1 public class LoginUserTest { 2     public String checkLogin(String username, String password){ 3         if (!(Objects.equals(username, "user"))){ 4             System.out.println("------執行1------"); 5             System.out.println("------執行2------"); 6             System.out.println("------執行3------"); 7             throw new LoginUserException("name is wrong");  //自訂異常資訊,throw的用法,產生一個異常對象LoginUserException 8         } 9         return "返回結果";10     }11 12 }

3、測試異常:

 1 public class ExceptionTest { 2     /** 3      * 測試自訂異常 4      */ 5     @Test 6     public void testCheckLogin(){ 7         LoginUserTest loginUserTest = new LoginUserTest(); 8         String s = loginUserTest.checkLogin("user1", ""); 9         System.out.println("----s:" + s);   //列印異常:exception.LoginUserException: name is wrong10     }11 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.