setUncaughtExceptionHandler线程异常获取
/** * @Description: * @author: fdy * @date: 2020/2/13 16:23 */ public class SetUncaughtExceptionHandlerTest { public static void main(String[] args) { Thread t = new Thread(new adminThread()); t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println(t + " throws exception: " + e); } }); // this will call run() function t.start(); } } class adminThread implements Runnable { @Override public void run() { throw new RuntimeException("运行期异常"); } }
结果: Thread[Thread-0,5,main] throws exception: java.lang.RuntimeException: 运行期异常
原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/java/lang/thread_setuncaughtexceptionhandler.html
其他: https://www.cnblogs.com/baiqiantao/p/79bf1e7f0803557c9195acec7af1e244.html
浙公网安备 33010602011771号