Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么合并结果不';如果返回一个失败的阶段,是否不能异常完成?_Java_Asynchronous_Completable Future_Completion Stage - Fatal编程技术网

Java 为什么合并结果不';如果返回一个失败的阶段,是否不能异常完成?

Java 为什么合并结果不';如果返回一个失败的阶段,是否不能异常完成?,java,asynchronous,completable-future,completion-stage,Java,Asynchronous,Completable Future,Completion Stage,调用的以下代码段未在处设置异常(它打印无异常): 但是,下面调用的类似代码确实设置了异常: CompletableFuture.completedFuture(true) .thenCompose( x -> { return CompletableFuture.failedStage(new RuntimeException()); }) .whenComplete( (myVal, myEx) -> {

调用的以下代码段未在处设置异常(它打印
无异常
):

但是,下面调用的类似代码确实设置了异常:

    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");
      }
    });

为什么当它的双功能实际返回一个失败的阶段时,
thenCombine
返回一个正常完成的
CompletionStage
。由
thenCombine
返回的
CompletableFuture
将与
CompletableFuture.failedStage(new RuntimeException())
一起完成

如果不连锁电话,情况会变得更清楚:

CompletableFuture first=CompletableFuture.completedFuture(true);
CompletableFuture second=CompletableFuture.completedFuture(true);
完全未来
    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");
      }
    });