<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                Java中異常也是以類,對象的形式存在 異常父類Throwable ①Error錯誤 ②Exception異常 *** **面試題:Error和Exception的區別** Error指的是比較嚴重錯誤,該錯誤無法通過代碼調試挽回 Exception指的是程序運行期間不應該出現的錯誤,通常稱為BUG,可以通過代碼修改避免異常 *** ### Exception異常 異常的繼承樹 ![](https://box.kancloud.cn/93d5b21f222df34938dfbf7925179d51_651x446.png) ①檢查性異常 編譯器會自動檢查,自動提示報錯 ②非檢查性異常 編譯器不會自動檢查(例如:數組越界,空指針等),都是因為思維不縝密或沒有按照編碼規范書寫造成的,這種情況應該由程序員自行避免。 *** ### 處理異常的兩種方式 ①自行處理 ~~~ public class ExceptionDemo1 { public void test() { // 將參數代表的類加載到JVM內存里 // 處理異常的兩種方式 // 1.自行處理try..catch..finally // try塊:將有拋出異常風險的代碼寫到try塊當中--相當于體檢中心 // try塊不能單獨存在,絕大多數情況下,與catch配合--相當于醫生 // catch塊可以存在多個,catch塊參數是捕獲的異常信息類型 // 思考:finally--書寫一定要執行的代碼:關閉流,回收資源 try { Class.forName("com.neuedu.chapter8_0426_1.ExceptionDemo1"); int arr[] = {1,2,3}; System.out.println(arr[11]); System.out.println(1111); return; } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(123); } } public static void main(String[] args) { // 數組越界 // int arr[] = {1,2,3}; // System.out.println(arr[13]); // 空指針 ExceptionDemo1 ed = new ExceptionDemo1(); // ed = null; ed.test(); } } ~~~ ②向上拋出 ~~~ public class ExceptionDemo2 { public void test() throws ClassNotFoundException,Exception{ // 2.向上拋出(規避異常) Class.forName("com.neuedu.chapter8_0426_1.ExceptionDemo1"); } public static void main(String[] args) { ExceptionDemo2 ed = new ExceptionDemo2(); try { ed.test(); } catch (Exception e) { e.printStackTrace(); } } } ~~~ *** ### 主動拋出異常throw ~~~ public class PersonTest { public static void main(String[] args) { Person p = new Person(); p.head = 9; p.arms = 1; // 主動觸發throw if(p.head != 1 || p.arms != 2) { try { throw new WrongHeadOrArmsException(); } catch (WrongHeadOrArmsException e) { e.printStackTrace(); } } } } ~~~ ### 自定義異常 ~~~ public class WrongHeadOrArmsException extends Exception{ } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看