<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 異常處理 > 用戶不正當的輸入 > 本身系統的問題 ## 為什么要處理異常 如果不處理異常,一旦在出現異常的地方,程序就會被「異常指令流」直接終止,不再往下運行。 ## 處理異常 **使用 try catch** > 判斷一個對象是否為空,使用 `null != str` 在程序方法設計,特別是方法的實現中,對于傳入的參數,是不可知的,所以要對傳入的參數進行最基礎的驗證后,才能使用,例如,判斷是否為 null。 ~~~ public static void fun1(String str, String subStr) { if (null != str && null != subStr) { if (str.indexOf(subStr) >= 0) { System.out.println("存在子串"); } else { System.out.println("不存在子串"); } } else { System.out.println("不合法字符串"); } } ~~~ 在 try catch 結構中,catch 是可以省略的,也可以多異常的捕獲,但是出現多異常的時候,父類異常只能出現在最下面。 在實際的開發中,一般最簡單的方式就是使用一個 Exception 直接處理。 **使用 finally** 無論是否出現異常都會被執行,特別要注意的是,如果沒有寫 catch ,那么 finally 是會被執行的,但是中斷后的語句是不會被執行的。 ~~~ public class Demo1 { public static void main(String[] args) { String str = "abc"; str = null; fun1("abc", null); } public static int div(int m, int n) { int k = m / n; return k; } public static void fun1(String str, String subStr) { try { System.out.println("11111111"); int i = 10 / 0; if (str.indexOf(subStr) >= 0) { System.out.println("存在子串"); } else { System.out.println("不存在子串"); } System.out.println("end"); } catch (Exception ex) { System.out.println("程序出現錯誤"); ex.printStackTrace(); System.out.println("#"+ex.getMessage()); } finally { // 一定會被執行的代碼塊 System.out.println("finally"); } System.out.println("fun1 end"); } } ~~~ **throws** 在**方法定義**的后面顯式的聲明方法是有異常的,調用該方法的程序是要顯式的處理異常,后者也可以再次拋出。 ~~~ public class Demo2 { public static void main(String[] args) { // fun1(); // fun2(); try { fun3(); } catch (Exception ex) { ex.printStackTrace(); } } public static void fun1() { System.out.println("begin"); int i = 0; try { i = 10 / 0; } catch(Exception ex) { ex.printStackTrace(); } System.out.println(i); System.out.println("end"); } // 使用 throws 拋出異常,通知調用該方法的程序,你要去處理。 public static void fun2() throws Exception{ System.out.println("begin"); int i = 0; i = 10 / 0; System.out.println(i); System.out.println("end"); } public static void fun3() throws Exception { System.out.println("begin"); int i = 0; try { i = 10 / 0; } catch (Exception ex){ // 不做處理 throw new Exception("系統錯誤"); } System.out.println(i); System.out.println("end"); } } ~~~ **throw** 自定義的創建一個異常對象。在一般的異常發生時候,虛擬機會動態的創建一個「系統異常」拋出,和自定義使用 throw 拋出是一個概念。 ~~~ public class Demo3 { public static void main(String[] args) throws Exception { fun1(17); System.out.println("end"); } public static void fun1(int age) throws Exception { if (age < 18) { throw new Exception("您的年齡不合法"); } else { System.out.println("您已經進入VIP房間"); } } } ~~~ ## 異常堆棧信息 - 異常類 - 異常提示信息:getMessage() - 異常所在的代碼位置:自下而上是異常出現的代碼所在方法的調用順序的先后。 ## 常見的異常 NPE:NullPointerException 在調用對象屬性或者方法的時候,對象其實是個 null,就會報此異常; java.lang.ArrayIndexOutOfBoundsException:數組越界 java.lang.IndexOutOfBoundsException: Index: 1, Size: 1:集合越界
                  <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>

                              哎呀哎呀视频在线观看