为什么不使用API提供的功能:
CompletableFuture<Object> failed = new CompletableFuture<>();
failed.completeExceptionally(new SomeException());
从
documentation
:
如果尚未完成,则导致调用get()和相关方法抛出给定的异常。
然而,
get()
将提高
ExecutionException
原因应该是
SomeException
,这意味着你的
getStatus
代码无效:
try {
return futureA.get();
} catch (ExecutionException e) {
// handle cause, eg: if (e.getCause() instanceof SomeException) ...
}
try {
return futureB.get();
} catch (ExecutionException e) {
// handle cause, eg: if (e.getCause() instanceof SomeException) ...
}
return null; // or whatever default value/exception