Thread nicht abgefangene Ausnahme UncaughtException erfordert UncaughtZExceptionHandler zur Behandlung
Warum müssen Sie also UncaughtZExceptionHandler verwenden?
Der Hauptthread kann den Thread problemlos erfassen, der Unterthread jedoch nicht
Wie Sie dem folgenden Code entnehmen können, ist der Hauptthread selbst dann nicht betroffen, wenn der Unterthread eine Ausnahme auslöst alle
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 * */
Wie Sie dem folgenden Code entnehmen können, gibt es keine Möglichkeit, Sub-Threads abzufangen, auch wenn es sinnlos ist, Catch zum Abfangen von Sub-Thread-Ausnahmen zu verwenden. Try Catch ist für den Haupt-Thread gedacht -Thread-Ausnahmen
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 * */
Ausnahmen können durch Try Catch in der Ausführungsmethode abgefangen werden, sind aber insbesondere problematisch, da Sie manuell Catch in jeder Ausführungsmethode ausprobieren müssen
UncaughtExceptionHandler anpassen
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 * */
Das obige ist der detaillierte Inhalt vonSo verwenden Sie die Java-Ausnahmebehandlung UncaughtExceptionHandler. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!