<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 功能強大 支持多語言、二開方便! 廣告
                ## Chapter 10. Exceptions(異常) ### Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors(對可恢復情況使用 checked 異常,對編程錯誤使用運行時異常) Java provides three kinds of throwables: checked exceptions, runtime exceptions, and errors. There is some confusion among programmers as to when it is appropriate to use each kind of throwable. While the decision is not always clear-cut, there are some general rules that provide strong guidance. Java 提供了三種可拋出項:checked 異常、運行時異常和錯誤。程序員們對什么時候使用這些可拋出項比較困惑。雖然決策并不總是明確的,但是有一些通用規則可以提供強有力的指導。 The cardinal rule in deciding whether to use a checked or an unchecked exception is this: **use checked exceptions for conditions from which the caller can reasonably be expected to recover.** By throwing a checked exception, you force the caller to handle the exception in a catch clause or to propagate it outward. Each checked exception that a method is declared to throw is therefore a potent indication to the API user that the associated condition is a possible outcome of invoking the method. 決定是使用 checked 異常還是 unchecked 異常的基本規則是:**使用 checked 異常的情況是為了合理地期望調用者能夠從中恢復。** 通過拋出一個 checked 的異常,你可以強制調用者在 catch 子句中處理異常,或者將其傳播出去。因此,方法中聲明的要拋出的每個 checked 異常,都清楚的向 API 用戶表明:the associated condition is a possible outcome of invoking the method. By confronting the user with a checked exception, the API designer presents a mandate to recover from the condition. The user can disregard the mandate by catching the exception and ignoring it, but this is usually a bad idea (Item 77). 通過向用戶提供 checked 異常,API 設計者提供了從條件中恢復的要求。用戶為了無視強制要求,可以捕獲異常并忽略,但這通常不是一個好主意([Item-77](/Chapter-10/Chapter-10-Item-77-Don’t-ignore-exceptions.md)) 。 There are two kinds of unchecked throwables: runtime exceptions and errors. They are identical in their behavior: both are throwables that needn’t, and generally shouldn’t, be caught. If a program throws an unchecked exception or an error, it is generally the case that recovery is impossible and continued execution would do more harm than good. If a program does not catch such a throwable, it will cause the current thread to halt with an appropriate error message. 有兩種 unchecked 的可拋出項:運行時異常和錯誤。它們在行為上是一樣的:都是可拋出的,通常不需要也不應該被捕獲。如果程序拋出 unchecked 異常或錯誤,通常情況下是不可能恢復的,如果繼續執行,弊大于利。如果程序沒有捕獲到這樣的可拋出項,它將導致當前線程停止,并發出適當的錯誤消息。 **Use runtime exceptions to indicate programming errors.** The great majority of runtime exceptions indicate precondition violations. A precondition violation is simply a failure by the client of an API to adhere to the contract established by the API specification. For example, the contract for array access specifies that the array index must be between zero and the array length minus one, inclusive. ArrayIndexOutOfBoundsException indicates that this precondition was violated. **使用運行時異常來指示編程錯誤。** 絕大多數運行時異常都表示操作違反了先決條件。違反先決條件是指使用 API 的客戶端未能遵守 API 規范所建立的約定。例如,數組訪問約定指定數組索引必須大于等于 0 并且小于等于 length-1 (length:數組長度)。ArrayIndexOutOfBoundsException 表示違反了此先決條件。 One problem with this advice is that it is not always clear whether you’re dealing with a recoverable conditions or a programming error. For example, consider the case of resource exhaustion, which can be caused by a programming error such as allocating an unreasonably large array, or by a genuine shortage of resources. If resource exhaustion is caused by a temporary shortage or by temporarily heightened demand, the condition may well be recoverable. It is a matter of judgment on the part of the API designer whether a given instance of resource exhaustion is likely to allow for recovery. If you believe a condition is likely to allow for recovery, use a checked exception; if not, use a runtime exception. If it isn’t clear whether recovery is possible, you’re probably better off using an unchecked exception, for reasons discussed in Item 71. 這個建議存在的問題是,并不總能清楚是在處理可恢復的條件還是編程錯誤。例如,考慮資源耗盡的情況,這可能是由編程錯誤(如分配一個不合理的大數組)或真正的資源短缺造成的。如果資源枯竭是由于暫時短缺或暫時需求增加造成的,這種情況很可能是可以恢復的。對于 API 設計人員來說,判斷給定的資源耗盡實例是否允許恢復是一個問題。如果你認為某個條件可能允許恢復,請使用 checked 異常;如果沒有,則使用運行時異常。如果不清楚是否可以恢復,最好使用 unchecked 異常,原因將在 [Item-71](/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md) 中討論。 While the Java Language Specification does not require it, there is a strong convention that errors are reserved for use by the JVM to indicate resource deficiencies, invariant failures, or other conditions that make it impossible to continue execution. Given the almost universal acceptance of this convention, it’s best not to implement any new Error subclasses. Therefore, **all of the unchecked throwables you implement should subclass RuntimeException** (directly or indirectly). Not only shouldn’t you define Error subclasses, but with the exception of AssertionError, you shouldn’t throw them either. 雖然 Java 語言規范沒有要求,但有一個約定俗成的約定,即錯誤保留給 JVM 使用,以指示:資源不足、不可恢復故障或其他導致無法繼續執行的條件。考慮到這種約定被大眾認可,所以最好不要實現任何新的 Error 子類。因此,**你實現的所有 unchecked 可拋出項都應該繼承 RuntimeException**(直接或間接)。不僅不應該定義 Error 子類,而且除了 AssertionError 之外,不應該拋出它們。 It is possible to define a throwable that is not a subclass of Exception, RuntimeException, or Error. The JLS doesn’t address such throwables directly but specifies implicitly that they behave as ordinary checked exceptions (which are subclasses of Exception but not RuntimeException). So when should you use such a beast? In a word, never. They have no benefits over ordinary checked exceptions and would serve merely to confuse the user of your API. 可以自定義一種可拋出的異常,它不是 Exception、RuntimeException 或 Error 的子類。JLS 不直接處理這些可拋出項,而是隱式地指定它們作為普通 checked 異常(普通 checked 異常是 Exception 的子類,但不是 RuntimeException 的子類)。那么,什么時候應該使用這樣的「猛獸」呢?總之,永遠不要。與普通 checked 異常相比,它們沒有任何好處,只會讓 API 的用戶感到困惑。 API designers often forget that exceptions are full-fledged objects on which arbitrary methods can be defined. The primary use of such methods is to provide code that catches the exception with additional information concerning the condition that caused the exception to be thrown. In the absence of such methods, programmers have been known to parse the string representation of an exception to ferret out additional information. This is extremely bad practice (Item 12). Throwable classes seldom specify the details of their string representations, so string representations can differ from implementation to implementation and release to release. Therefore, code that parses the string representation of an exception is likely to be nonportable and fragile. API 設計人員常常忘記異常是成熟對象,可以為其定義任意方法。此類方法的主要用途是提供捕獲異常的代碼,并提供有關引發異常的附加信息。如果缺乏此類方法,程序員需要自行解析異常的字符串表示以獲取更多信息。這是極壞的做法([Item-12](/Chapter-3/Chapter-3-Item-12-Always-override-toString.md))。這種類很少指定其字符串表示的細節,因此字符串表示可能因實現而異,也可能因版本而異。因此,解析異常的字符串表示形式的代碼可能是不可移植且脆弱的。 Because checked exceptions generally indicate recoverable conditions, it’s especially important for them to provide methods that furnish information to help the caller recover from the exceptional condition. For example, suppose a checked exception is thrown when an attempt to make a purchase with a gift card fails due to insufficient funds. The exception should provide an accessor method to query the amount of the shortfall. This will enable the caller to relay the amount to the shopper. See Item 75 for more on this topic. 因為 checked 異常通常表示可恢復的條件,所以這類異常來說,設計能夠提供信息的方法來幫助調用者從異常條件中恢復尤為重要。例如,假設當使用禮品卡購物由于資金不足而失敗時,拋出一個 checked 異常。該異常應提供一個訪問器方法來查詢差額。這將使調用者能夠將金額傳遞給購物者。有關此主題的更多信息,請參見 [Item-75](/Chapter-10/Chapter-10-Item-75-Include-failure-capture-information-in-detail-messages.md)。 To summarize, throw checked exceptions for recoverable conditions and unchecked exceptions for programming errors. When in doubt, throw unchecked exceptions. Don’t define any throwables that are neither checked exceptions nor runtime exceptions. Provide methods on your checked exceptions to aid in recovery. 總而言之,為可恢復條件拋出 checked 異常,為編程錯誤拋出 unchecked 異常。當有疑問時,拋出 unchecked 異常。不要定義任何既不是 checked 異常也不是運行時異常的自定義異常。應該為 checked 異常設計相關的方法,如提供異常信息,以幫助恢復。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-10/Chapter-10-Introduction.md)** - **Previous Item(上一條目):[Item 69: Use exceptions only for exceptional conditions(僅在確有異常條件下使用異常)](/Chapter-10/Chapter-10-Item-69-Use-exceptions-only-for-exceptional-conditions.md)** - **Next Item(下一條目):[Item 71: Avoid unnecessary use of checked exceptions(避免不必要地使用 checked 異常)](/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md)**
                  <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>

                              哎呀哎呀视频在线观看