多线程中异常需显式处理:1. 线程内try-catch捕获;2. 设置UncaughtExceptionHandler捕获未处理异常;3. Callable通过Future.get()抛出ExecutionException;4. 全局默认处理器防止异常丢失。

在Java多线程环境中,直接使用try-catch无法捕获线程内部抛出的异常,因为每个线程有独立的执行栈。主线程中的try-catch不能捕获子线程中发生的异常。要正确处理多线程异常,需采用特定策略。
1. 在线程内部使用try-catch
最直接的方式是在线程的run方法内部添加try-catch块,自行处理异常:
- 适用于Runnable实现方式
- 可记录日志、通知主线程或进行资源清理
示例代码:
new Thread(() -> {
try {
// 可能出错的操作
int result = 10 / 0;
} catch (Exception e) {
System.err.println("子线程捕获异常: " + e.getMessage());
}
}).start();
2. 使用UncaughtExceptionHandler全局捕获
为线程设置未捕获异常处理器,处理所有未被捕获的异常:
立即学习“Java免费学习笔记(深入)”;
- 可通过Thread.setUncaughtExceptionHandler()设置
- 适合统一的日志记录或监控场景
示例:
Thread thread = new Thread(() -> {
throw new RuntimeException("线程内异常");
});
thread.setUncaughtExceptionHandler((t, e) ->
System.err.println(t.getName() + " 发生异常: " + e.getMessage())
);
thread.start();
3. 使用Callable和Future获取异常
使用ExecutorService提交Callable任务时,异常会在调用get()时以ExecutionException抛出:
- Callable支持返回值和抛出异常
- 主线程可通过Future.get()捕获异常
示例:
ExecutorService executor = Executors.newSingleThreadExecutor(); Futurefuture = executor.submit(() -> { return 10 / 0; }); try { future.get(); // 此处抛出ExecutionException } catch (ExecutionException e) { System.err.println("任务执行异常: " + e.getCause().getMessage()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } executor.shutdown();
4. 设置默认全局异常处理器
通过Thread.setDefaultUncaughtExceptionHandler()为所有未设置处理器的线程指定默认行为:
- 通常在程序启动时设置一次
- 防止异常静默丢失
示例:
Thread.setDefaultUncaughtExceptionHandler((t, e) ->
System.err.println("全局捕获: " + t.getName() + " 异常 " + e)
);
基本上就这些。关键是要明白:线程内的异常不会自动传播到创建者线程,必须显式处理。结合try-catch、UncaughtExceptionHandler和Future机制,可以构建健壮的多线程异常处理方案。










