CompletableFuture:whenCompleteAsync() 不允许我重新抛出异常
我是 CompletableFuture 世界的新手。我正在尝试做一些负面测试,以允许我故意抛出异常的方式。该异常将决定通过/失败。
这是代码片段:
protected CompletableFuture<Response> executeAsync(@NonNull Supplier<Response> call) {
return CompletableFuture
.supplyAsync(call::get)
.whenCompleteAsync((response, exception) -> {
if (exception == null) {
try {
Helper.throwIfNotExpected(clientProperties.getName(), response, null);
} catch (ServiceException e) {
throw new ServiceException(null,e.getMessage(),null);
}
} else {
log.error("Async API call failed.", exception);
}
});
}
这给了我一个错误,在 catch 部分显示未处理的异常。我查找了示例和文档,但找不到有关 SupplyAsync/whenCompleteAsync 中异常处理的更多信息。提前致谢。
I am new to the world of CompletableFuture. I am trying to do some negative tests, in a way that will allow me to throw an exception intentionally. This exception will decide the PASS/FAIL.
Here's the code snippet:
protected CompletableFuture<Response> executeAsync(@NonNull Supplier<Response> call) {
return CompletableFuture
.supplyAsync(call::get)
.whenCompleteAsync((response, exception) -> {
if (exception == null) {
try {
Helper.throwIfNotExpected(clientProperties.getName(), response, null);
} catch (ServiceException e) {
throw new ServiceException(null,e.getMessage(),null);
}
} else {
log.error("Async API call failed.", exception);
}
});
}
This gives me an error saying unhandled exception in catch part. I looked up examples and documentation but could not find much information about Exception handling in supplyAsync/whenCompleteAsync. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决检查例外的
dompletableFuture
的缺点的一种好方法是将另一个postimablefuture
实例委托。就您的示例而言,它看起来像是A good way to work around the shortcoming of
CompletableFuture
regarding checked exceptions is to delegate to anotherCompletableFuture
instance. For your example it would look like something along the lines of