<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國際加速解決方案。 廣告
                # Java 異常處理 > 原文: [https://beginnersbook.com/2013/04/exception-handling-examples/](https://beginnersbook.com/2013/04/exception-handling-examples/) 在本教程中,我們將看到幾個常用異常的示例。如果您正在尋找異常處理教程,請參閱此完整指南:[Java 中的異常處理](https://beginnersbook.com/2013/04/java-exception-handling/)。 ## 示例 1:算術異常 類:`Java.lang.ArithmeticException` 這是`java.lang`包中的內置類。當整數除以零時會發生此異常。 ```java class Example1 { public static void main(String args[]) { try{ int num1=30, num2=0; int output=num1/num2; System.out.println ("Result: "+output); } catch(ArithmeticException e){ System.out.println ("You Shouldn't divide a number by zero"); } } } ``` **以上程序的輸出:** ```java You Shouldn't divide a number by zero ``` **說明:**在上面的例子中,我將整數除以零,因此拋出`ArithmeticException`。 ## 示例 2:`ArrayIndexOutOfBounds`異常 類:`Java.lang.ArrayIndexOutOfBoundsException` 當您嘗試訪問不存在的數組索引時,會發生此異常。例如,如果數組只有 5 個元素,并且我們試圖顯示第 7 個元素,那么它將拋出此異常。 ```java class ExceptionDemo2 { public static void main(String args[]) { try{ int a[]=new int[10]; //Array has only 10 elements a[11] = 9; } catch(ArrayIndexOutOfBoundsException e){ System.out.println ("ArrayIndexOutOfBounds"); } } } ``` **輸出:** ```java ArrayIndexOutOfBounds ``` 在上面的示例中,數組被初始化為僅存儲 10 個元素索引 0 到 9.因為我們嘗試訪問索引 11 的元素,所以程序拋出此異常。 ## 示例 3:`NumberFormatException` 分類:`Java.lang.NumberFormatException` 將字符串解析為任何數字變量時會發生此異常。 例如,語句`int num=Integer.parseInt ("XYZ");`將拋出`NumberFormatException`,因為`String "XYZ"`無法解析為`int`。 ```java class ExceptionDemo3 { public static void main(String args[]) { try{ int num=Integer.parseInt ("XYZ") ; System.out.println(num); }catch(NumberFormatException e){ System.out.println("Number format exception occurred"); } } } ``` 輸出: ```java Number format exception occurred ``` ## 示例 4:`StringIndexOutOfBound`異常 分類:`Java.lang.StringIndexOutOfBoundsException` * 每當調用一個不在范圍內的字符串的索引時,就會創建此類的對象。 * 字符串對象的每個字符都存儲在從 0 開始的特定索引中。 * 要獲得字符串的特定索引中存在的字符,我們可以使用[`java.lang.String`](https://beginnersbook.com/2013/12/java-strings/)的[`charAt(int)`方法](https://beginnersbook.com/2013/12/java-strings/),其中`int`參數是索引。 例如。 ```java class ExceptionDemo4 { public static void main(String args[]) { try{ String str="beginnersbook"; System.out.println(str.length());; char c = str.charAt(0); c = str.charAt(40); System.out.println(c); }catch(StringIndexOutOfBoundsException e){ System.out.println("StringIndexOutOfBoundsException!!"); } } } ``` 輸出: ```java 13 StringIndexOutOfBoundsException!! ``` 發生異常是因為`String`中沒有引用的索引。 ## 示例 5:`NullPointer`異常 類:`Java.lang.NullPointer Exception` 只要使用`null`對象調用成員,就會創建此類的對象。 ```java class Exception2 { public static void main(String args[]) { try{ String str=null; System.out.println (str.length()); } catch(NullPointerException e){ System.out.println("NullPointerException.."); } } } ``` 輸出: ```java NullPointerException.. ``` 這里,`length()`是函數,應該在對象上使用。但是在上面的示例中`String`對象`str`為空,因此它不是由于`NullPointerException`發生的對象。
                  <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>

                              哎呀哎呀视频在线观看