线程中运行时异常处理
A线程调用B线程,B线程发生异常,A怎么知道B线程发生异常了
public class Demo { public static void main(String[] args) throws Exception, SecurityException { Thread t = new Thread(() -> { // int i = 1 / 0; //这里只能抛出运行时异常,如果是检查异常,会提示try-catch throw new RuntimeException("发生异常了"); }); t.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println(t.getName()); System.out.println(e.getMessage()); } }); t.start(); } }
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

更多精彩