<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 捕獲線程運行期間的異常 上一篇文章我們學習了在我們使用application的時候在他出現問題或者人為終止的時候,我們怎么有機會捕獲異常或者人為的做一些操作,比如寫一些日志,或者發送一個RESTful或者關閉釋放一些資源,那么線程運行期間的異常該如何捕獲呢?我們知道線程的運行邏輯單元在run方法里,run方法的簽名是不允許拋出異常的,今天我們就來學習一下線程的Exception。 先來一段拋出異常的代碼: ```java public class ThreadException { private final static int A = 10; private static final int B = 0; public static void main(String[] args) { /// 捕獲線程運行期間的異常 Thread t = new Thread(() -> { try { Thread.sleep(1_000L); int result = A / B; System.out.println("result = " + result); } catch (InterruptedException e) { e.printStackTrace(); } }); t.start(); } } ``` 運行效果如下: ![](https://img.kancloud.cn/96/64/96641da4d88883720f51cc37420eee0e_856x228.gif) 我們改造一下我們的代碼,Thread對象提供了`setUncaughtExceptionHandler`方法用來獲取線程中產生的異常。而且建議使用該方法為線程設置異常捕獲方法: ```java t.setUncaughtExceptionHandler((thread, e) -> { System.out.println(e); System.out.println(thread.getName()); System.out.println(thread.getThreadGroup().getName()); System.out.println(thread.getThreadGroup().getParent().getName()); }); ``` 運行效果如下: ![](https://img.kancloud.cn/a1/34/a13447f8c0fbb7d6b044a58d90b8ee27_856x228.gif) 可以看到異常的詳細堆棧信息已經打印出來了,下面做個實驗來輸出一下,先建幾個類分別進行調用,如下: ```java public class ThreadException { public static void main(String[] args) { // stack trace new Test1().test(); } } ``` ```java public class Test1 { private Test2 test2 = new Test2(); public void test() { test2.test(); } } ``` ```java public class Test2 { public void test() { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); Stream.of(stackTrace) .filter(e -> !e.isNativeMethod()) .forEach(e -> Optional.of(e.getClassName() + "." + e.getMethodName() + ":" + e.getLineNumber()) .ifPresent(System.out::println) ); } } ``` 運行效果如下: ![](https://img.kancloud.cn/e4/1e/e41e13d118730f1ba2236107dea0ced5_856x228.gif) 這個在后續使用中有補充再繼續補充,后面開始學習ThreadGroupApI的學習,然后實現一個線程池,java并發編程的基礎就碼一遍了。
                  <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>

                              哎呀哎呀视频在线观看