CompletableFuture.completedFuture() returns a CompletedFuture
that is already completed with the given value.
How do we construct a CompletableFuture
that is already completed exceptionally?
Meaning, instead of returning a value I want the future to throw an exception.
I just found this:
CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(t);
There is no static factory method, but the default constructor seems to do the job.
Java 9 provides CompletableFuture.failedFuture(Throwable ex)
that does exactly that.
来源:https://stackoverflow.com/questions/49116855/completablefuture-already-completed-with-an-exception