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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## Chapter 10. Exceptions(異常) ### Item 77: Don’t ignore exceptions(不要忽略異常) While this advice may seem obvious, it is violated often enough that it bears repeating. When the designers of an API declare a method to throw an exception, they are trying to tell you something. Don’t ignore it! It is easy to ignore exceptions by surrounding a method invocation with a try statement whose catch block is empty: 雖然這一建議似乎顯而易見,但它經常被違反,因此值得強調。當 API 的設計人員聲明一個拋出異常的方法時,他們試圖告訴你一些事情。不要忽略它!如果在方法調用的周圍加上一條 try 語句,其 catch 塊為空,可以很容易忽略異常: ``` // Empty catch block ignores exception - Highly suspect! try { ... } catch (SomeException e) { } ``` **An empty catch block defeats the purpose of exceptions,** which is to force you to handle exceptional conditions. Ignoring an exception is analogous to ignoring a fire alarm—and turning it off so no one else gets a chance to see if there’s a real fire. You may get away with it, or the results may be disastrous. Whenever you see an empty catch block, alarm bells should go off in your head. **空 catch 塊違背了異常的目的,** 它的存在是為了強制你處理異常情況。忽略異常類似于忽略火災警報一樣,關掉它之后,其他人就沒有機會看到是否真的發生了火災。你可能僥幸逃脫,但結果可能是災難性的。每當你看到一個空的 catch 塊,你的腦海中應該響起警報。 There are situations where it is appropriate to ignore an exception. For example, it might be appropriate when closing a FileInputStream. You haven’t changed the state of the file, so there’s no need to perform any recovery action, and you’ve already read the information that you need from the file, so there’s no reason to abort the operation in progress. It may be wise to log the exception, so that you can investigate the matter if these exceptions happen often. **If you choose to ignore an exception, the catch block should contain a comment explaining why it is appropriate to do so, and the variable should be named ignored:** 在某些情況下,忽略異常是合適的。例如,在關閉 FileInputStream 時,忽略異常可能是合適的。你沒有更改文件的狀態,因此不需要執行任何恢復操作,并且已經從文件中讀取了所需的信息,因此沒有理由中止正在進行的操作。記錄異常可能是明智的,這樣如果這些異常經常發生,你應該研究起因。**如果你選擇忽略異常,catch 塊應該包含一條注釋,解釋為什么這樣做是合適的,并且應該將變量命名為 ignore:** ``` Future<Integer> f = exec.submit(planarMap::chromaticNumber); int numColors = 4; // Default; guaranteed sufficient for any map try { numColors = f.get(1L, TimeUnit.SECONDS); } catch (TimeoutException | ExecutionException ignored) { // Use default: minimal coloring is desirable, not required } ``` The advice in this item applies equally to checked and unchecked exceptions. Whether an exception represents a predictable exceptional condition or a programming error, ignoring it with an empty catch block will result in a program that continues silently in the face of error. The program might then fail at an arbitrary time in the future, at a point in the code that bears no apparent relation to the source of the problem. Properly handling an exception can avert failure entirely. Merely letting an exception propagate outward can at least cause the program to fail swiftly, preserving information to aid in debugging the failure. 本條目中的建議同樣適用于 checked 異常和 unchecked異常。不管異常是表示可預測的異常條件還是編程錯誤,用空 catch 塊忽略它將導致程序在錯誤面前保持靜默。然后,程序可能會在未來的任意時間點,在與問題源沒有明顯關系的代碼中失敗。正確處理異常可以完全避免失敗。僅僅讓異常向外傳播,可能會導致程序走向失敗,保留信息有利于調試。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-10/Chapter-10-Introduction.md)** - **Previous Item(上一條目):[Item 76: Strive for failure atomicity(盡力保證故障原子性)](/Chapter-10/Chapter-10-Item-76-Strive-for-failure-atomicity.md)** - **Next Item(下一條目):[Chapter 11 Introduction(章節介紹)](/Chapter-11/Chapter-11-Introduction.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>

                              哎呀哎呀视频在线观看