为什么不能在try-catch中捕获子线程的异常 ?
一、基础概念
在 Java中,主线程不能直接捕获子线程抛出的异常的!主要是因为子线程和主线程是独立的执行单元, 它们的执行是并发的,因此主线程无法捕获子线程的异常。子线程的异常通常由子线程自己处理或通过适当的异常处理机制处理。
那么也就是说,以下代码是无法生效的
public class Main {
public static void main(String[] args) {
Thread childThread = new Thread(() -> {
try {
// 子线程抛出异常
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("子线程捕获到异常");
throw e;
}
});
try {
// 主线程等待子线程执行完成
childThread.start();
} catch (InterruptedException e) {
System.out.println("主线程捕获到异常");
}
System.out.println("主线程继续执行");
}
}
以上代码输出结果:

说明子线程中发生的异常,在主线程中是无法直接捕获的。但是也不是毫无办法的 !
二、如何实现主线程捕获子线程异常

浙公网安备 33010602011771号