如何捕获线程运行期间的异常
import java.util.concurrent.TimeUnit; public class ThreadException { public static void main(String[] args) { Thread t = new Thread(() -> { try { TimeUnit.SECONDS.sleep(3); int res = 1/0; } catch (InterruptedException e) { e.printStackTrace(); } }, "error"); t.setUncaughtExceptionHandler((thread, e)->{ System.out.println(e); System.out.println(thread); }); t.start(); } }
输出结果:
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

更多精彩