CompletableFuture already completed with an exception

前端 未结 2 1046
时光说笑
时光说笑 2020-12-01 18:27

CompletableFuture.completedFuture() returns a CompletedFuture that is already completed with the given value.

How do we construct a CompletableFut

相关标签:
2条回答
  • 2020-12-01 18:53

    Unlike Java 9 and later, Java 8 does not provide a static factory method for this scenario. The default constructor can be used instead:

    CompletableFuture<T> future = new CompletableFuture<>();
    future.completeExceptionally(exception);
    
    0 讨论(0)
  • 2020-12-01 18:58

    Java 9 provides CompletableFuture.failedFuture​(Throwable ex) that does exactly that.

    0 讨论(0)
提交回复
热议问题