<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國際加速解決方案。 廣告
                ## Chapter 10. Exceptions(異常) ### Item 75: Include failure capture information in detail messages(異常詳細消息中應包含捕獲失敗的信息) When a program fails due to an uncaught exception, the system automatically prints out the exception’s stack trace. The stack trace contains the exception’s string representation, the result of invoking its toString method. This typically consists of the exception’s class name followed by its detail message. Frequently this is the only information that programmers or site reliability engineers will have when investigating a software failure. If the failure is not easily reproducible, it may be difficult or impossible to get any more information. Therefore, it is critically important that the exception’s toString method return as much information as possible concerning the cause of the failure. In other words, the detail message of an exception should capture the failure for subsequent analysis. 當程序由于未捕獲異常而失敗時,系統可以自動打印出異常的堆棧跟蹤。堆棧跟蹤包含異常的字符串表示,這是調用其 toString 方法的結果。這通常包括異常的類名及其詳細信息。通常,這是程序員或管理員在調查軟件故障時所掌握的唯一信息。如果失敗不容易重現,想獲得更多的信息會非常困難。因此,異常的 toString 方法返回盡可能多的關于失敗原因的信息是非常重要的。換句話說,由失敗導致的異常的詳細信息應該被捕獲,以便后續分析。 **To capture a failure, the detail message of an exception should contain the values of all parameters and fields that contributed to the exception.** For example, the detail message of an IndexOutOfBoundsException should contain the lower bound, the upper bound, and the index value that failed to lie between the bounds. This information tells a lot about the failure. Any or all of the three values could be wrong. The index could be one less than the lower bound or equal to the upper bound (a “fencepost error”), or it could be a wild value, far too low or high. The lower bound could be greater than the upper bound (a serious internal invariant failure). Each of these situations points to a different problem, and it greatly aids in the diagnosis if you know what sort of error you’re looking for. **要捕獲失敗,異常的詳細消息應該包含導致異常的所有參數和字段的值。** 例如,IndexOutOfBoundsException 的詳細消息應該包含下界、上界和未能位于下界之間的索引值。這些信息說明了很多關于失敗的信息。這三個值中的任何一個或所有值都可能是錯誤的。索引可以小于或等于上界(「越界錯誤」),也可以是一個無效值,太小或太大。下界可能大于上界(嚴重的內部故障)。每一種情況都指向一個不同的問題,如果你知道你在尋找什么樣的錯誤,這對診斷有很大的幫助。 One caveat concerns security-sensitive information. Because stack traces may be seen by many people in the process of diagnosing and fixing software issues, **do not include passwords, encryption keys, and the like in detail messages.** 提及一個與安全敏感信息有關的警告。因為許多人在診斷和修復軟件問題的過程中可能會看到堆棧跟蹤,所以 **不應包含密碼、加密密鑰等詳細信息。** While it is critical to include all of the pertinent data in the detail message of an exception, it is generally unimportant to include a lot of prose. The stack trace is intended to be analyzed in conjunction with the documentation and, if necessary, source code. It generally contains the exact file and line number from which the exception was thrown, as well as the files and line numbers of all other method invocations on the stack. Lengthy prose descriptions of the failure are superfluous; the information can be gleaned by reading the documentation and source code. 雖然在異常的詳細信息中包含所有相關數據非常重要,但通常不需要包含大量的描述。堆棧跟蹤將與文檔一起分析,如果需要,還將與源代碼一起分析。它通常包含拋出異常的確切文件和行號,以及堆棧上所有方法調用的文件和行號。冗長的描述對一個失敗問題而言是多余的;可以通過閱讀文檔和源代碼來收集信息。 The detail message of an exception should not be confused with a user-level error message, which must be intelligible to end users. Unlike a user-level error message, the detail message is primarily for the benefit of programmers or site reliability engineers, when analyzing a failure. Therefore, information content is far more important than readability. User-level error messages are often localized, whereas exception detail messages rarely are. One way to ensure that exceptions contain adequate failure-capture information in their detail messages is to require this information in their constructors instead of a string detail message. The detail message can then be generated automatically to include the information. For example, instead of a String constructor, IndexOutOfBoundsException could have had a constructor that looks like this: 異常的詳細信息不應該與用戶層的錯誤消息混淆,因為用戶層錯誤消息最終必須被用戶理解。與用戶層錯誤消息不同,詳細消息主要是為程序員或管理員在分析故障時提供的。因此,信息內容遠比可讀性重要。用戶層錯誤消息通常是本地化的,而異常詳細信息消息很少本地化。確保異常在其詳細信息中包含足夠的故障捕獲信息的一種方法是,在其構造函數中配置,而不是以傳入字符串方式引入這些信息。之后可以自動生成詳細信息來包含細節。例如,IndexOutOfBoundsException 構造函數不包含 String 參數,而是像這樣: ``` /** * Constructs an IndexOutOfBoundsException. ** @param lowerBound the lowest legal index value * @param upperBound the highest legal index value plus one * @param index the actual index value */ public IndexOutOfBoundsException(int lowerBound, int upperBound, int index) { // Generate a detail message that captures the failure super(String.format("Lower bound: %d, Upper bound: %d, Index: %d",lowerBound, upperBound, index)); // Save failure information for programmatic access this.lowerBound = lowerBound; this.upperBound = upperBound; this.index = index; } ``` As of Java 9, IndexOutOfBoundsException finally acquired a constructor that takes an int valued index parameter, but sadly it omits the lowerBound and upperBound parameters. More generally, the Java libraries don’t make heavy use of this idiom, but it is highly recommended. It makes it easy for the programmer throwing an exception to capture the failure. In fact, it makes it hard for the programmer not to capture the failure! In effect, the idiom centralizes the code to generate a high-quality detail message in the exception class, rather than requiring each user of the class to generate the detail message redundantly. 從 Java 9 開始,IndexOutOfBoundsException 最終獲得了一個接受 int 值索引參數的構造函數,但遺憾的是它忽略了下界和上界參數。更一般地說,Java 庫不會大量使用這個習慣用法,但是強烈推薦使用它。它使程序員很容易通過拋出異常來捕獲失敗。事實上,它使程序員不想捕獲失敗都難!實際上,這個習慣用法將集中在異常類中生成高質量的詳細信息,而不是要求該類的每個用戶都生成冗余的詳細信息。 **譯注:IndexOutOfBoundsException 有關 int 參數的構造函數源碼** ``` /** * Constructs a new {@code IndexOutOfBoundsException} class with an * argument indicating the illegal index. * * <p>The index is included in this exception's detail message. The * exact presentation format of the detail message is unspecified. * * @param index the illegal index. * @since 9 */ public IndexOutOfBoundsException(int index) { super("Index out of range: " + index); } ``` As suggested in Item 70, it may be appropriate for an exception to provide accessor methods for its failure-capture information (lowerBound, upperBound, and index in the above example). It is more important to provide such accessor methods on checked exceptions than unchecked, because the failure-capture information could be useful in recovering from the failure. It is rare (although not inconceivable) that a programmer might want programmatic access to the details of an unchecked exception. Even for unchecked exceptions, however, it seems advisable to provide these accessors on general principle (Item 12, page 57). 正如 [Item-70](/Chapter-10/Chapter-10-Item-70-Use-checked-exceptions-for-recoverable-conditions-and-runtime-exceptions-for-programming-errors.md) 中建議的,異常為其故障捕獲信息提供訪問器方法是適合的(上面示例中的下界、上界和索引)。在 checked 異常上提供此類訪問器方法比 unchecked 異常上提供此類訪問器方法更為重要,因為故障捕獲信息可能有助于程序從故障中恢復。程序員可能希望通過編程訪問 unchecked 異常的詳細信息,但這是很少見的(盡管是可以想象的)。然而,即使對于 unchecked 異常,根據一般原則,提供這些訪問器也是可以的([Item-12](/Chapter-3/Chapter-3-Item-12-Always-override-toString.md),第 57 頁)。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-10/Chapter-10-Introduction.md)** - **Previous Item(上一條目):[Item 74: Document all exceptions thrown by each method(為每個方法記錄會拋出的所有異常)](/Chapter-10/Chapter-10-Item-74-Document-all-exceptions-thrown-by-each-method.md)** - **Next Item(下一條目):[Item 76: Strive for failure atomicity(盡力保證故障原子性)](/Chapter-10/Chapter-10-Item-76-Strive-for-failure-atomicity.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>

                              哎呀哎呀视频在线观看