0Pricing
Java Academy · Lesson

Error Handling and Async HTTP Pipeline

Handle errors with exceptionally, handle, and whenComplete, then build an async HTTP client pipeline.

Why Async Error Handling Matters

In async pipelines, exceptions don't propagate the same way as in synchronous code. You must explicitly handle errors using exceptionally, handle, or whenComplete.

exceptionally: Recovering From Errors

exceptionally(Function<Throwable, T>) is called only if the future completes with an exception. It provides a fallback value, keeping the pipeline alive.

CompletableFuture<String> safe = fetchUser(id)
    .thenApply(User::getName)
    .exceptionally(ex -> {
        log.warn("Fetch failed: " + ex.getMessage());
        return "Anonymous";
    });
System.out.println(safe.join()); // "Anonymous" if fetch failed

All lessons in this course

  1. Creating and Completing CompletableFutures
  2. Chaining with thenApply and thenCompose
  3. Combining Futures: allOf and anyOf
  4. Error Handling and Async HTTP Pipeline
← Back to Java Academy