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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 中的`try-catch` - 異常處理 > 原文: [https://beginnersbook.com/2013/04/try-catch-in-java/](https://beginnersbook.com/2013/04/try-catch-in-java/) 在[上一篇教程](https://beginnersbook.com/2013/04/java-exception-handling/)中,我們討論了什么是異常處理以及我們為什么這樣做。在本教程中,我們將看到用于異常處理的`try-catch`塊。 ## `try`塊 `try`塊包含可以發生異常的一組語句。`try`塊后面總是跟一個`catch`塊,它處理相關`try`塊中發生的異常。`try`塊必須后跟`catch`塊或`finally`塊或兩者。 ### `try`塊的語法 ```java try{ //statements that may cause an exception } ``` > 在編寫程序時,如果您認為程序中的某些語句可以拋出異常,請將它們包含在`try`塊中并處理該異常 ## `catch`塊 `catch`塊是處理異常的地方,此塊必須遵循`try`塊。單個`try`塊可以有幾個與之關聯的`catch`塊。您可以在不同的`catch`塊中捕獲不同的異常。當`try`塊中發生異常時,將執行處理該特定異常的相應`catch`塊。例如,如果在`try`塊中發生算術異常,則執行`catch`塊中用于算術異常的語句。 ### java 中`try-catch`的語法 ```java try { //statements that may cause an exception } catch (exception(type) e(object))? { //error handling code } ``` ## 示例:`try-catch`塊 如果`try`塊中發生異常,則執行控制將傳遞給相應的`catch`塊。單個`try`塊可以有多個與之關聯的`catch`塊,您應該放置`catch`塊,使得通用異常處理程序`catch`塊位于最后(參見下面的示例)。 通用異常處理程序可以處理所有異常但是你應該放在最后,如果你把它放在所有`catch`塊之前,那么它將顯示通用消息。您始終希望為每種類型的異常提供有意義的消息,而不是通用消息。 ```java class Example1 { public static void main(String args[]) { int num1, num2; try { ? /* We suspect that this block of statement can throw * exception so we handled it by placing these statements * inside try and handled the exception in catch block */ num1 = 0; num2 = 62 / num1; System.out.println(num2); System.out.println("Hey I'm at the end of try block"); } ? catch (ArithmeticException e) {? /* This block will only execute if any Arithmetic exception * occurs in try block */ System.out.println("You should not divide a number by zero"); } catch (Exception e) { /* This is a generic Exception handler which means it can handle * all the exceptions. This will execute if the exception is not * handled by previous catch blocks. */ System.out.println("Exception occurred"); } System.out.println("I'm out of try-catch block in Java."); } } ``` 輸出: ```java You should not divide a number by zero I'm out of try-catch block in Java. ``` ## Java 中的多個`catch`塊 我們在上面看到的示例是有多個`catch`塊,讓我們在示例的幫助下看到關于多個`catch`塊的一些規則。要詳細閱讀,請參閱[在 java 中捕獲多個異常](https://beginnersbook.com/2013/05/catch-multiple-exceptions/)。 1. 如上所述,單個`try`塊可以包含任意數量的`catch`塊。 2. 通用`catch`塊可以處理所有異常。無論是`ArrayIndexOutOfBoundsException`還是`ArithmeticException`或`NullPointerException`或任何其他類型的異常,它都會處理所有這些異常。要查看`NullPointerException`和`ArrayIndexOutOfBoundsException`的示例,請參閱以下文章:[異常處理示例程序](https://beginnersbook.com/2013/04/exception-handling-examples/)。 ```java catch(Exception e){ ? //This catch block catches all the exceptions } ``` > 如果你想知道為什么我們需要其他捕獲處理程序,當我們有一個可以處理所有的通用。這是因為在通用異常處理程序中,您可以顯示消息,但您不確定它可能觸發的異常類型,因此它將為所有異常顯示相同的消息,并且用戶可能無法理解發生了哪個異常。這就是你應該放置的原因是在所有特定異常`catch`塊的末尾 3. 如果`try`塊中沒有異常,則完全忽略`catch`塊。 4. 對應的`catch`塊執行特定類型的異常: `catch(ArithmeticException e)`是一個可以解決`ArithmeticException`的`catch`塊 `catch(NullPointerException e)`是一個可以處理`NullPointerException`的`catch`塊 5. 你也可以拋出異常,這是一個高級主題,我在單獨的教程中介紹了它:[用戶定義異常](https://beginnersbook.com/2013/04/user-defined-exception-in-java/),[`throw`關鍵字](https://beginnersbook.com/2013/12/throws-keyword-example-in-java/),[`throw` vs `throws`](https://beginnersbook.com/2013/04/difference-between-throw-and-throws-in-java/)。 ### 多個`catch`塊的示例 ```java class Example2{ public static void main(String args[]){ try{ int a[]=new int[7]; a[4]=30/0; System.out.println("First print statement in try block"); } catch(ArithmeticException e){ System.out.println("Warning: ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Warning:?ArrayIndexOutOfBoundsException"); } catch(Exception e){ System.out.println("Warning: Some Other exception"); } System.out.println("Out of try-catch block..."); } } ``` 輸出: ```java Warning:?ArithmeticException Out of try-catch block... ``` 在上面的示例中,有多個`catch`塊,當`try`塊中發生異常時,這些`catch`塊按順序執行。這意味著如果你把最后一個`catch`塊(`catch(Exception e)`)放在第一個地方,就在`try`塊之后,那么在任何異常的情況下,這個塊將執行,因為它可以處理所有異常。該擋塊應放在最后,以避免這種情況。 ## `finally`塊 我在這里單獨介紹了這個:[java `finally`塊](https://beginnersbook.com/2013/04/java-finally-block/)。現在你只需知道這個塊執行是否發生異常。您應該將這些語句放在`finally`塊中,必須執行是否發生異常。
                  <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>

                              哎呀哎呀视频在线观看