<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://javabeginnerstutorial.com/core-java-tutorial/exception-handling-try-catch-java/](https://javabeginnerstutorial.com/core-java-tutorial/exception-handling-try-catch-java/) 作為開發人員,我們每天處理風險情況。 服務器故障,或者沒有足夠的空間分配堆上的對象,或者給定位置不存在文件,依此類推。 因此,每次我們決定采取有風險的措施時,我們都必須通知編譯器我們知道這是有風險的事情,并且已經準備好處理它。 我們如何處理這不是編譯器的問題。 它需要記住的是,我們正在照顧可能出現的任何特殊情況。 我們通過將代碼包裝在 Java 的`try catch`中來處理這些情況。 ### `try/catch/finally`塊的基本語法: ```java try{ //code that could throw an exception //if exception thrown, following code is not reachable //control jumps to catch block }catch(ExceptionType referenceVariable){ //code that is executed only when an exception is thrown //does something using the exception reference variable //usually prints stack trace or exception description }finally{ //cleanup code //always executes regardless of an exception } ``` ### 注意 1. 在`try`塊和`catch`塊之間不能編寫任何代碼。 2. `try`塊必須緊隨其后的是`catch`或`finally`塊,或二者兼而有之。 如果沒有`catch`塊,則盡管`finally`方法具有`try/finally`,但`final`方法應**聲明異常**。 3. 您不能擁有不帶`catch`或`finally`的`try`塊。 4. 如果您不想在代碼中處理異常,請使用**引發并聲明**子句。 誰調用您的代碼都必須使用`try/catch`塊來處理它。 ## 控制流 1. 如果`try`塊成功,即未引發異常,則控制將移至`finally`塊(如果存在)。 跳過`catch`塊。 在沒有`finally`塊的情況下,將執行`catch`塊下面的任何代碼。 2. 如果`try`塊失敗(發生異常),則控制權轉移到處理異常的`catch`塊。 `try`塊中的其余代碼永遠不會執行。 如果存在`finally`塊,則在`catch`塊執行完成后運行。 3. 如果`try/catch`塊**具有**返回語句,那么即使執行`finally`塊! 流控制首先跳轉到`finally`塊,然后返回`return`語句。 ### 示例 ```java public class TryCatch1 { public static void main(String[] args) { System.out.println(riskyAction()); } public static String riskyAction(){ try{ System.out.println("Started executing try block"); return "returning from try block"; }catch(Exception e){ return "returning from catch blcok"; }finally{ System.out.println("print statement from finally"); } } } ``` ### 輸出 ```java Started executing try block print statement from finally returning from try block ``` ### 解釋 1. `try`塊運行并打印“開始執行`try`塊”。 2. 一旦遇到`return`語句,流程將立即轉移到`finally`塊并打印“`finally`的`print`語句”。 3. `finally`塊執行完成后,控制權返回`try`塊中的`return`語句,并返回“從`try`塊返回”。 4. 如果`finally`塊具有**返回語句**,則來自`try/catch`塊的`return`語句將被覆蓋。 ### 示例 ```java public class TryCatch2{ public static void main(String[] args) { System.out.println(riskyAction("hello")); System.out.println("-----------"); System.out.println(riskyAction("howdy")); } public static String riskyAction(String greeting){ try{ if(greeting.equals("hello")){ System.out.println(greeting + " from try block"); }else{ throw new Exception(); } return "returning from try block"; }catch(Exception e){ System.out.println(greeting + " from catch block"); return "returning from catch block"; }finally{ return "returning from finally block"; } } } ``` ### 輸出 ```java hello from try block returning from finally block ----------- howdy from catch block returning from finally block ``` ### 解釋 對于方法調用,`riskyAction("hello")`:`try`塊成功并打印`try`塊中的“hello”。 由于它具有`return`語句,因此控制權轉移到`finally`塊。 `finally`塊還具有一個`return`語句,該語句將覆蓋`try`塊中的語句,因此該方法將返回并打印“`finally`塊返回”到控制臺。 對于`riskyAction("howdy")`:`try`塊引發異常,該異常在 catch 塊中處理,該異常打印`catch`塊中的“howdy”。 就像我們在`try`塊成功的情況下看到的一樣,`finally`塊的`return`語句也覆蓋`catch`塊中的`return`語句。 結果,該方法返回并打印“從`finally`塊返回”到控制臺。 **注意**:由于無論是否發生異常,`finally`塊總是被執行,因此,如果它具有`return`語句,則可以預期到意外的結果,并且可能變得難以調試。 **作為一種好的做法,最好避免在`finally`塊中編寫`return`語句。** ## 捕獲多個異常 在 Java 7 之前,為了處理多個異常,使用了多個`catch`塊(從最特定到最普通)。 編寫代碼是為了打印棧跟蹤,執行錯誤恢復,鏈接異常,允許用戶做出決定等。但是編寫多個`catch`塊包含許多**重復代碼**。 另外,程序員傾向于捕獲更廣泛或更普遍的異常,而不是特定的異常。 例如,捕獲`IOException`而不是`FileNotFoundException`。 從 **Java SE 7 和更高版本**起,這些缺陷已通過**單`catch`塊**解決,該塊可以**處理多種類型的異常**。 在這里,要處理的異常類型在以豎線(`|`)分隔的`catch`子句的主題中指定。 #### 示例 ```java catch(ArrayIndexOutOfBoundsException | SQLException ex){ ex.printStackTrace(); } ``` #### 注意 1. 每當單個`catch`塊處理多個異常時,引用變量(上例中為“`ex`”)為`final`,因此將其視為常量。 因此,無法為其分配其他任何值。 在某些情況下,這限制了異常處理能力。 2. 不能將異常類型與其父類組合在一起,因為子類異常由于已經被捕獲而變得不可訪問。 #### 示例 ![multiple exceptions](https://img.kancloud.cn/89/fa/89fa7101596e609c5a270e5b4595bccc_627x115.png) ## 仔細查看`Finally`塊 1. 一旦`try`塊中的控件退出,無論是否引發異常,都將始終執行`finally`塊。 2. `finally`塊未執行, 1. 如果在執行`try/catch`塊代碼時 JVM 退出 2. 如果在控制權到達`finally`塊之前執行了`System.exit()` 3. 如果執行`try/catch`代碼的線程被中斷或殺死 3. `finally`塊通過關閉可能已打開的資源來防止任何資源泄漏。 4. 如果要恢復任何資源,則必須將代碼放在`finally`塊中。 5. 僅帶有`finally`塊的`try`塊(即沒有`catch`塊)仍應聲明該異常以進行處理。 在 **Java SE 7 和更高版本中,考慮使用`try-with-resources`語句**自動關閉不再使用的資源。 開發人員不需要為此記住通過編寫`finally`塊來釋放使用的資源。 [`try-with-resources`語句的文章](https://javabeginnerstutorial.com/core-java-tutorial/exception-handling-try-resources/)提供了詳細的信息以及其優勢,摘要和示例代碼片段。
                  <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>

                              哎呀哎呀视频在线观看