标签:一个 vat rup 自己 runnable default throws runtime 利用
public class ExceptionInChildThread implements Runnable { @Override public void run() { throw new RuntimeException(); } public static void main(String[] args) { new Thread(new ExceptionInChildThread()).start(); for (int i = 0; i < 1000; i++) { System.out.println(i); } } }
public class CantCatchDirectly implements Runnable { @Override public void run() { throw new RuntimeException(); } public static void main(String[] args) { try { new Thread(new CantCatchDirectly(), "thread1").start(); Thread.sleep(100); new Thread(new CantCatchDirectly(), "thread2").start(); Thread.sleep(100); new Thread(new CantCatchDirectly(), "thread3").start(); } catch (InterruptedException e) { System.out.println("捕获到异常"); } } }
import java.util.logging.Level; import java.util.logging.Logger; public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { private String name; public MyUncaughtExceptionHandler(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 UseOwnUncaughtExceptionHandler implements Runnable { @Override public void run() { throw new RuntimeException(); } public static void main(String[] args) throws InterruptedException { Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler("捕获器1")); new Thread(new CantCatchDirectly(), "thread1").start(); Thread.sleep(100); new Thread(new CantCatchDirectly(), "thread2").start(); Thread.sleep(100); new Thread(new CantCatchDirectly(), "thread3").start(); } }
标签:一个 vat rup 自己 runnable default throws runtime 利用
原文地址:https://www.cnblogs.com/zhihaospace/p/12519575.html