在多线程开发中,我们会面对很多的异常问题。但是,主线程的异常我们知道很方便捕获并且处理,但是对于子线程而言呢?
package Thread_.UncaughtException;
/**
* @program:多线程和IO
* @descripton:传统方法无法捕获子线程异常
* @author:ZhengCheng
* @create:2021/9/22-10:13
**/
public class ThreadException extends Thread {
public static void main(String[] args) {
try {
new ThreadException().start();
} catch (Exception e) {
System.out.println("I caught this Exception");
}
}
// @Override
// public void run() {
// throw new RuntimeException();
// }
//解决方案1: (不推荐) 在run方法中使用trycatch捕获异常 ,提高健壮性。
@Override
public void run() {
try {
throw new RuntimeException();
} catch (RuntimeException e) {
System.out.println("catch success");
}
}
}
测试上述代码,在主线程中,我们是无法catch到子线程抛出的异常的。故子线程的异常&#