使用handle()的CompletableFuture异常处理

时间:2018-02-27 17:32:22

标签: java exception exception-handling

我一直在关注一些CompletableFuture指南,但我无法弄清楚以下代码有什么问题:

public void function1() throws CustomException {
    thingsThatCouldThrowAnException();
}

public void function2() throws CustomException {
    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
        otherThings();
        function1();
        return "text";
    }).handle((result, ex) -> {
        if (result != null) {
            doThings();
            return result;
        } else {
            handleException(ex);
            return null;
        }
    });
}

我仍然得到error: unreported exception CustomException; must be caught or declared to be thrown。我滥用CompletableFuture.handle(),还是其他的东西?谢谢!

0 个答案:

没有答案