Callable.call()抛出的异常会被封装进Future,调用future.get()时以ExecutionException包装抛出,需主动捕获并用getCause()获取原始异常。

在多线程中使用 Callable 时,call() 方法若抛出异常,**不会直接在调用线程崩溃,而是被封装进 Future 对象中,需主动获取才能暴露**。关键不是“阻止异常”,而是“正确捕获和处理它”。
异常会被 Future.get() 重新抛出
Callable.call() 中的检查异常(如 IOException)和运行时异常(如 NullPointerException)都会被 FutureTask 捕获,并在调用 future.get() 时以 ExecutionException 包装后抛出。其 getCause() 返回原始异常。
- 必须调用
get()才会触发异常传播;不调用就永远“静默” - 即使
call()抛的是RuntimeException,get()仍抛ExecutionException -
get(long, TimeUnit)超时时抛TimeoutException,与业务异常无关
标准做法:在 get() 外层 try-catch ExecutionException
这是最常见也最推荐的方式,把异常处理逻辑集中在结果消费处:
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> {
if (Math.random() > 0.5) throw new RuntimeException("模拟失败");
return "success";
});
try {
String result = future.get(); // 这里才真正抛出 ExecutionException
System.out.println(result);
} catch (ExecutionException e) {
Throwable cause = e.getCause(); // 获取原始异常
System.err.println("任务执行失败:" + cause.getMessage());
// 根据 cause 类型做差异化处理,比如重试、降级、记录告警等
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 恢复中断状态
System.err.println("任务被中断");
} finally {
executor.shutdown();
}
提前检查 Future 状态避免盲等
如果不想阻塞等待,可用 isDone() 或 isCancelled() 判断任务是否完成/取消,再决定是否调用 get():
-
future.isDone()为true表示已结束(成功、异常或取消) -
future.isCancelled()为true表示被取消,此时get()抛CancellationException - 结合
get(1, TimeUnit.SECONDS)做带超时的非阻塞获取,适合响应式场景
批量提交时统一处理多个 Future 的异常
用 invokeAll() 提交一组 Callable,返回 List<Future<T>>。遍历每个 Future 并调用 get(),逐个捕获异常:
List<Callable<Integer>> tasks = Arrays.asList(
() -> { throw new RuntimeException("task1 failed"); },
() -> 42,
() -> { throw new IOException("task3 io error"); }
);
List<Future<Integer>> futures = executor.invokeAll(tasks);
for (Future<Integer> f : futures) {
try {
Integer result = f.get();
System.out.println("成功:" + result);
} catch (ExecutionException e) {
System.err.println("单个任务失败:" + e.getCause().getMessage());
}
}
不复杂但容易忽略:异常不在 call 里消失,也不在线程池里被吞掉——它安静地躺在 Future 里,等你伸手去拿。

















