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实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄