Java SE API
CompletableFuture.failedFuture()
Create a future already completed exceptionally.
Formal Syntax & Structure
java syntaxjava
static <U> CompletableFuture<U> failedFuture(Throwable ex)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| ex | Throwable | Failure reason |
Working Example
Example Snippetjava
import java.util.concurrent.*;import java.util.concurrent.atomic.*;import java.util.concurrent.locks.*;public class Main {
public static void main(String[] args) throws Exception { CompletableFuture<Integer> failed = CompletableFuture.failedFuture(new IllegalArgumentException("bad input")); try { failed.join(); } catch (CompletionException e) { System.out.println(e.getCause().getClass().getSimpleName()); } try { failed.get(1, TimeUnit.SECONDS); } catch (ExecutionException e) { System.out.println(e.getCause().getMessage()); } }}