例子
Thread thread = new Thread(() -> {
System.out.println("thread in");
int res = 1 / 0;
System.out.println("thread out");
});
thread.start();
thread.setUncaughtExceptionHandler((t,e) -> {
System.out.println(t.getName() + "has error :" + e.getMessage());
});
结果
thread in
Exception in thread "Thread-0" java.lang.ArithmeticException: / by zero
at com.example.pig.service.thread.ThreadTest.lambda$main$0(ThreadTest.java:13)
at java.lang.Thread.run(Thread.java:748)
Process finished with exit code 0