Java 的 CompletableFuture : ExecutionException 的时间
使用 CompletableFuture 计算值时出现的任何潜在错误都会通过 get() 方法抛出ExecutionException
。我当然可以在调用此方法时记录此异常,但这会产生误导,因为此异常可能发生在与当前完全不同的时间。
简而言之,我想要有关ExecutionException
何时发生的信息。 有什么优雅的解决方案吗?
When working with CompletableFuture any potential errors from computing the value are exposed via the get() method throwing an ExecutionException
. I can of course log this exception at the time of calling this method, but it would be misleading as this exception may have happened at a completely different time from the current.
In short, I would like information on when the ExecutionException
occurred.
Is there any elegant solution to this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用回调方法,即例外情况下的whenComplete 或handle,它们传递Throwable 和结果。
Use callback methods, i.e. exceptionally, whenComplete or handle, which pass a Throwable and a result.
尝试将执行代码包装到try-catch中,然后将其传递给完整的future。
Try to wrap execution code into try-catch, which you passed to CompletableFuture.