首頁 >Java >java教程 >Java異常處理UncaughtExceptionHandler如何使用

Java異常處理UncaughtExceptionHandler如何使用

PHPz
PHPz轉載
2023-04-14 21:55:012092瀏覽

異常處理

線程未捕獲異常 UncaughtException 需要UncaughtZExceptionHandler 來進行處理

那麼為什麼非要用UncaughtZExceptionHandler呢?

  • 主線程可以輕鬆捕獲線程,子線程不可以

  • 從下面程式碼可知,即使子執行緒拋出異常,主線程也絲毫不受影響

public class ChildException implements Runnable{
    public static void main(String[] args) {
        new Thread(new ChildException()).start();
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
    @Override
    public void run() {
        throw new RuntimeException();
    }
}
/*
* Exception in thread "Thread-0" java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.ChildException.run(ChildException.java:14)
   at java.lang.Thread.run(Thread.java:748)
0
1
2
3
4
5
6
7
8
9
* */
  • 從下面程式碼可知,即使想用catch捕獲子執行緒異常,時沒有用的

  • try catch 是針對主線程的,沒有辦法捕獲子線程的異常

public class CantCatch implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        try {
            new Thread(new CantCatch(), "thread0").start();
            Thread.sleep(300);
            new Thread(new CantCatch(), "thread1").start();
            Thread.sleep(300);
            new Thread(new CantCatch(), "thread2").start();
            Thread.sleep(300);
            new Thread(new CantCatch(), "thread3").start();
            Thread.sleep(300);
        } catch (RuntimeException e) {
            System.out.println("catch");
        }
    }
    @Override
    public void run() {
        throw new RuntimeException();
    }
}
/*
* Exception in thread "thread0" java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
   at java.lang.Thread.run(Thread.java:748)
Exception in thread "thread1" java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
   at java.lang.Thread.run(Thread.java:748)
Exception in thread "thread2" java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
   at java.lang.Thread.run(Thread.java:748)
Exception in thread "thread3" java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
   at java.lang.Thread.run(Thread.java:748)
Process finished with exit code 0
* */

在run方法中進行try catch可以捕獲到異常,但是特別麻煩,因為需要手動地在每個run方法中都進行try catch

UncaughtExceptionHandler

自訂UncaughtExceptionHandler

public class MyUncaughtHandler implements Thread.UncaughtExceptionHandler{
    private String name;
    public MyUncaughtHandler(String name) {
        this.name = name;
    }
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Logger logger = Logger.getAnonymousLogger();
        logger.log(Level.WARNING, "线程异常" + t.getName(), e);
        System.out.println(name + "捕获" + t.getName()+ e);
    }
}

使用自訂的類別來捕獲異常

public class UseOwnExceptionHandler implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtHandler("MyHandler"));
//        try {
        new Thread(new UseOwnExceptionHandler(), "thread0").start();
        Thread.sleep(300);
        new Thread(new UseOwnExceptionHandler(), "thread1").start();
        Thread.sleep(300);
        new Thread(new UseOwnExceptionHandler(), "thread2").start();
        Thread.sleep(300);
        new Thread(new UseOwnExceptionHandler(), "thread3").start();
        Thread.sleep(300);
//        } catch (RuntimeException e) {
//            System.out.println("catch");
//        }
    }
    @Override
    public void run() {
//        try {
        throw new RuntimeException();
//        } catch (RuntimeException e) {
//            System.out.println("e");
//        }
    }
}
/*
* 一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 线程异常thread0
java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
   at java.lang.Thread.run(Thread.java:748)
MyHandler捕获thread0java.lang.RuntimeException
一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 线程异常thread1
java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
   at java.lang.Thread.run(Thread.java:748)
MyHandler捕获thread1java.lang.RuntimeException
一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 线程异常thread2
java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
   at java.lang.Thread.run(Thread.java:748)
MyHandler捕获thread2java.lang.RuntimeException
一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 线程异常thread3
java.lang.RuntimeException
   at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
   at java.lang.Thread.run(Thread.java:748)
MyHandler捕获thread3java.lang.RuntimeException
Process finished with exit code 0
* */

以上是Java異常處理UncaughtExceptionHandler如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除