以下代码片段调用thenCombine ,并没有在whenComplete设置异常(它打印No exception
):
CompletableFuture.completedFuture(true)
.thenCombine(CompletableFuture.completedFuture(true),
(x,y) -> {
return CompletableFuture.failedStage(new RuntimeException());
})
.whenComplete( (myVal, myEx) -> {
if (myEx == null) {
System.out.println("No exception");
} else {
System.out.println("There was an exception");
}
});
但是,下面调用thenCompose的类似代码确实设置了一个异常:
CompletableFuture.completedFuture(true)
.thenCompose(
x -> {
return CompletableFuture.failedStage(new RuntimeException());
})
.whenComplete( (myVal, myEx) -> {
if (myEx == null) {
System.out.println("No exception");
} else {
System.out.println("There was an exception");
}
});
为什么在 BiFunction 实际上返回失败阶段时thenCombine
返回正常完成?CompletionStage