CompletableFuture:whenCompleteAsync() 不允许我重新抛出异常

发布于 2025-01-17 15:01:49 字数 956 浏览 3 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(1

吃颗糖壮壮胆 2025-01-24 15:01:49

解决检查例外的dompletableFuture的缺点的一种好方法是将另一个postimablefuture实例委托。就您的示例而言,它看起来像是

protected CompletableFuture<Response> executeAsync(Supplier<Response> call) {
    CompletableFuture<Response> delegate = new CompletableFuture<>();

    CompletableFuture
        .supplyAsync(call)
        .whenCompleteAsync((response, exception) -> {
            if (exception == null) {
                try {
                    Helper.throwIfNotExpected(clientProperties.getName(), response, null);
                    delegate.complete(response);
                } catch (ServiceException e) {
                    delegate.completeExceptionally(new ServiceException(null,e.getMessage(),null));
                }
            } else {
                log.error("Async API call failed.", exception);
                delegate.completeExceptionally(exception);
            }
        });

    return delegate;
}

A good way to work around the shortcoming of CompletableFuture regarding checked exceptions is to delegate to another CompletableFuture instance. For your example it would look like something along the lines of

protected CompletableFuture<Response> executeAsync(Supplier<Response> call) {
    CompletableFuture<Response> delegate = new CompletableFuture<>();

    CompletableFuture
        .supplyAsync(call)
        .whenCompleteAsync((response, exception) -> {
            if (exception == null) {
                try {
                    Helper.throwIfNotExpected(clientProperties.getName(), response, null);
                    delegate.complete(response);
                } catch (ServiceException e) {
                    delegate.completeExceptionally(new ServiceException(null,e.getMessage(),null));
                }
            } else {
                log.error("Async API call failed.", exception);
                delegate.completeExceptionally(exception);
            }
        });

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