Java 的 CompletableFuture : ExecutionException 的时间

发布于 2025-01-18 10:24:05 字数 516 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮凉 2025-01-25 10:24:05

使用回调方法,即例外情况下的whenComplete 或handle,它们传递Throwable 和结果。

Use callback methods, i.e. exceptionally, whenComplete or handle, which pass a Throwable and a result.

忘年祭陌 2025-01-25 10:24:05

尝试将执行代码包装到try-catch中,然后将其传递给完整的future。

Try to wrap execution code into try-catch, which you passed to CompletableFuture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文