mem*_*und 5 java java-stream completable-future
问题:如何直接从 抛出自定义异常.exceptionally()?
List<CompletableFuture<Object>> futures =
tasks.stream()
.map(task -> CompletableFuture.supplyAsync(() -> businessLogic(task))
.exceptionally(ex -> {
if (ex instanceof BusinessException) return null;
//TODO how to throw a custom exception here??
throw new BadRequestException("at least one async task had an exception");
}))
.collect(Collectors.toList());
try {
List<Object> results = futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
} catch (CompletionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e.getCause());
}
Run Code Online (Sandbox Code Playgroud)
问题:我总是得到一个CompletionExceptionwho ex.getCause()is instanceof BadRequestException。
这可能吗?
正如Didier L 所说,函数抛出的异常(或者通常是完成a 的异常CompletableFuture)总是包含在 a 中CompletionException(除非它们已经是 aCompletionException或CancellationException)。
但请注意,即使不尝试通过exceptionally以下方式翻译异常,您的代码也会变得更加简单:
List<CompletableFuture<Object>> futures =
tasks.stream()
.map(task -> CompletableFuture.supplyAsync(() -> businessLogic(task)))
.collect(Collectors.toList());
try {
List<Object> results = futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());
} catch (CompletionException e) {
throw e.getCause() instanceof BusinessException?
new BadRequestException("at least one async task had an exception"): e;
}
Run Code Online (Sandbox Code Playgroud)
或者
… catch (CompletionException e) {
throw e.getCause() instanceof BusinessException?
new BadRequestException("at least one async task had an exception"):
e.getCause() instanceof BusinessException? (RuntimeException)e.getCause(): e;
}
Run Code Online (Sandbox Code Playgroud)
由于exceptionally的主要目的是将异常转换为非异常结果值,因此使用它来将异常转换为另一个抛出的异常并不是最合适的,它也需要一个instanceof. 因此,在catch子句中执行此翻译可以避免另一个翻译步骤。
| 归档时间: |
|
| 查看次数: |
6387 次 |
| 最近记录: |