<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### [把“被檢查的異常”轉換為“不檢查的異常”](https://lingcoder.gitee.io/onjava8/#/book/15-Exceptions?id=%e6%8a%8a%e8%a2%ab%e6%a3%80%e6%9f%a5%e7%9a%84%e5%bc%82%e5%b8%b8%e8%bd%ac%e6%8d%a2%e4%b8%ba%e4%b8%8d%e6%a3%80%e6%9f%a5%e7%9a%84%e5%bc%82%e5%b8%b8) 當編寫自己使用的簡單程序時,從`main()`中拋出異常是很方便的,但這并不總是有用。真正的問題是,當在一個普通方法里調用別的方法時發現:“我不知道該如何處理這個異常,但是不能把它'吞掉'或者打印一些無用的消息。”有了異常鏈,一個簡單的解決辦法就出現了。可以通過將一個“被檢查的異常”傳遞給`RuntimeException`的構造器,從而將它包裝進`RuntimeException`里,就像這樣: ~~~ try { // ... to do something useful } catch(IDontKnowWhatToDoWithThisCheckedException e) { throw new RuntimeException(e); } ~~~ 如果想把“被檢查的異常”這種功能“屏蔽”掉的話,這看上去像是一個好辦法。不用“吞下”異常,也不必把它放到方法的異常說明里面,而異常鏈還能保證你不會丟失任何原始異常的信息。 這種技巧給了你一種選擇,你可以不寫 try-catch 子句和/或異常說明,直接忽略異常,讓它自己沿著調用棧往上“冒泡”,同時,還可以用 getCause() 捕獲并處理特定的異常,就像這樣: ~~~ // exceptions/TurnOffChecking.java // "Turning off" Checked exceptions import java.io.*; class WrapCheckedException { void throwRuntimeException(int type) { try { switch(type) { case 0: throw new FileNotFoundException(); case 1: throw new IOException(); case 2: throw new RuntimeException("Where am I?"); default: return; } } catch(IOException | RuntimeException e) { // Adapt to unchecked: throw new RuntimeException(e); } } } class SomeOtherException extends Exception {} public class TurnOffChecking { public static void main(String[] args) { WrapCheckedException wce = new WrapCheckedException(); // You can call throwRuntimeException() without // a try block, and let RuntimeExceptions // leave the method: wce.throwRuntimeException(3); // Or you can choose to catch exceptions: for(int i = 0; i < 4; i++) try { if(i < 3) wce.throwRuntimeException(i); else throw new SomeOtherException(); } catch(SomeOtherException e) { System.out.println( "SomeOtherException: " + e); } catch(RuntimeException re) { try { throw re.getCause(); } catch(FileNotFoundException e) { System.out.println( "FileNotFoundException: " + e); } catch(IOException e) { System.out.println("IOException: " + e); } catch(Throwable e) { System.out.println("Throwable: " + e); } } } } ~~~ 輸出為: ~~~ FileNotFoundException: java.io.FileNotFoundException IOException: java.io.IOException Throwable: java.lang.RuntimeException: Where am I? SomeOtherException: SomeOtherException ~~~ `WrapCheckedException.throwRuntimeException()`包含可生成不同類型異常的代碼。這些異常被捕獲并包裝進`RuntimeException`對象,所以它們成了這些運行時異常的原因("cause")。 在 TurnOfChecking 里,可以不用 try 塊就調用 throwRuntimeException(),因為它沒有拋出“被檢查的異常”。但是,當你準備好去捕獲異常的時候,還是可以用 try 塊來捕獲任何你想捕獲的異常的。應該捕獲 try 塊肯定會拋出的異常,這里就是 SomeOtherException,RuntimeException 要放到最后去捕獲。然后把 getCause() 的結果(也就是被包裝的那個原始異常)拋出來。這樣就把原先的那個異常給提取出來了,然后就可以用它們自己的 catch 子句進行處理。 這種把被檢查的異常用`RuntimeException`包裝起來的技術,將在本書余下部分使用。另一種解決方案是創建自己的`RuntimeException`的子類。這樣的話,異常捕獲將不被強制要求,但是任何人都可以在需要的時候捕獲這些異常。
                  <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>

                              哎呀哎呀视频在线观看