提供一个子线程的异常统一处理方法
/**
* <Description>
* 全局子线程异常处理器
* @author Jun Lee
* @version 1.0
* @createDate 2020/01/08 21:40
* @see com.thread.exception
*/
public class ThreadExceptionHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.out.println(t.getName()+"捕捉到了异常信息:"+e.getMessage());
}
}
/**
* <Description>
*
* @author Jun Lee
* @version 1.0
* @createDate 2020/01/08 21:39
* @see com.thread.exception
*/
public class ThreadException implements Runnable {
public void run() {
throw new RuntimeException("出现异常了");
}
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new ThreadExceptionHandler());
new Thread(new ThreadException()).start();
new Thread(new ThreadException()).start();
new Thread(new ThreadException()).start();
new Thread(new ThreadException()).start();
}
}
通过上述代码可以将子线程的异常信息进行统一处理