为什么不能在try Catch中捕获子线程的异常_
为什么不能在try Catch中捕获子线程的异常_
为什么不能在try-catch中捕获子线程的异常?
典型回答
在Java中,主线程不能直接捕获子线程抛出的异常的!主要是因为子线程和主线程是独立的执行单元,它们的执行是并发的,因此主线程无法捕获子线程的异常。子线程的异常通常由子线程自己处理或通过适当的异常处理机制处理。
线程隔离,这也是Java保证线程出现异常不会影响整个进程的一个主要原理。
那么也就是说,以下代码是无法生效的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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("主线程继续执行");
}
}
以上代码输出结果:
1
2
子线程捕获到异常
主线程继续执行
说明子线程中发生的异常,在主线程中是无法直接捕获的。但是也不是毫无办法的!
扩展知识
如何实现主线程捕获子线程异常
This post is licensed under
CC BY 4.0
by the author.