<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### [重新拋出異常](https://lingcoder.gitee.io/onjava8/#/book/15-Exceptions?id=%e9%87%8d%e6%96%b0%e6%8a%9b%e5%87%ba%e5%bc%82%e5%b8%b8) 有時希望把剛捕獲的異常重新拋出,尤其是在使用 Exception 捕獲所有異常的時候。既然已經得到了對當前異常對象的引用,可以直接把它重新拋出: ~~~ catch(Exception e) { System.out.println("An exception was thrown"); throw e; } ~~~ 重拋異常會把異常拋給上一級環境中的異常處理程序,同一個 try 塊的后續 catch 子句將被忽略。此外,異常對象的所有信息都得以保持,所以高一級環境中捕獲此異常的處理程序可以從這個異常對象中得到所有信息。 如果只是把當前異常對象重新拋出,那么 printStackTrace() 方法顯示的將是原來異常拋出點的調用棧信息,而并非重新拋出點的信息。要想更新這個信息,可以調用 fillInStackTrace() 方法,這將返回一個 Throwable 對象,它是通過把當前調用棧信息填入原來那個異常對象而建立的,就像這樣: ~~~ // exceptions/Rethrowing.java // Demonstrating fillInStackTrace() public class Rethrowing { public static void f() throws Exception { System.out.println( "originating the exception in f()"); throw new Exception("thrown from f()"); } public static void g() throws Exception { try { f(); } catch(Exception e) { System.out.println( "Inside g(), e.printStackTrace()"); e.printStackTrace(System.out); throw e; } } public static void h() throws Exception { try { f(); } catch(Exception e) { System.out.println( "Inside h(), e.printStackTrace()"); e.printStackTrace(System.out); throw (Exception)e.fillInStackTrace(); } } public static void main(String[] args) { try { g(); } catch(Exception e) { System.out.println("main: printStackTrace()"); e.printStackTrace(System.out); } try { h(); } catch(Exception e) { System.out.println("main: printStackTrace()"); e.printStackTrace(System.out); } } } ~~~ 輸出為: ~~~ originating the exception in f() Inside g(), e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.f(Rethrowing.java:8) at Rethrowing.g(Rethrowing.java:12) at Rethrowing.main(Rethrowing.java:32) main: printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.f(Rethrowing.java:8) at Rethrowing.g(Rethrowing.java:12) at Rethrowing.main(Rethrowing.java:32) originating the exception in f() Inside h(), e.printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.f(Rethrowing.java:8) at Rethrowing.h(Rethrowing.java:22) at Rethrowing.main(Rethrowing.java:38) main: printStackTrace() java.lang.Exception: thrown from f() at Rethrowing.h(Rethrowing.java:27) at Rethrowing.main(Rethrowing.java:38) ~~~ 調用 fillInStackTrace() 的那一行就成了異常的新發生地了。 有可能在捕獲異常之后拋出另一種異常。這么做的話,得到的效果類似于使用 fillInStackTrace(),有關原來異常發生點的信息會丟失,剩下的是與新的拋出點有關的信息: ~~~ // exceptions/RethrowNew.java // Rethrow a different object from the one you caught class OneException extends Exception { OneException(String s) { super(s); } } class TwoException extends Exception { TwoException(String s) { super(s); } } public class RethrowNew { public static void f() throws OneException { System.out.println( "originating the exception in f()"); throw new OneException("thrown from f()"); } public static void main(String[] args) { try { try { f(); } catch(OneException e) { System.out.println( "Caught in inner try, e.printStackTrace()"); e.printStackTrace(System.out); throw new TwoException("from inner try"); } } catch(TwoException e) { System.out.println( "Caught in outer try, e.printStackTrace()"); e.printStackTrace(System.out); } } } ~~~ 輸出為: ~~~ originating the exception in f() Caught in inner try, e.printStackTrace() OneException: thrown from f() at RethrowNew.f(RethrowNew.java:16) at RethrowNew.main(RethrowNew.java:21) Caught in outer try, e.printStackTrace() TwoException: from inner try at RethrowNew.main(RethrowNew.java:26) ~~~ 最后那個異常僅知道自己來自 main(),而對 f() 一無所知。 永遠不必為清理前一個異常對象而擔心,或者說為異常對象的清理而擔心。它們都是用 new 在堆上創建的對象,所以垃圾回收器會自動把它們清理掉。
                  <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>

                              哎呀哎呀视频在线观看