Chaining After handle
What you'll learn: How to continue building async pipelines after using handle, and why the chain always proceeds normally unless you explicitly throw an exception.
The Handle Guarantee
When you call handle on a CompletableFuture, it always completes normally with whatever value your BiFunction returns—even if the upstream stage threw an exception. This is fundamentally different from exceptionally, which only runs on failure.
This guarantee means you can confidently chain more operations after handle without worrying about unhandled exceptions breaking your pipeline.
Building Pipelines After handle
Once handle processes both success and error cases, the returned CompletableFuture holds your transformed result. You can chain any standard methods afterward:
CompletableFuture<String> result = supplyAsync(() -> riskyOperation())
.handle((value, ex) -> {
if (ex != null) {
return "fallback";
}
return value.toUpperCase();
})
.thenApply(str -> str + " processed")
.thenAccept(System.out::println);
The thenApply and thenAccept stages see a normal, non-exceptional CompletableFuture. Whether riskyOperation() succeeded or failed, handle produced a String result.
The Exception Case
If your handle function itself throws an exception, that new exception propagates downstream:
supplyAsync(() -> 42)
.handle((value, ex) -> {
throw new IllegalStateException("Oops in handle!");
})
.exceptionally(ex -> {
// Catches the IllegalStateException from handle
return -1;
});
This is the only way to propagate failure after handle.
Key Takeaway: After handle, your pipeline continues normally with the returned value—allowing you to chain thenApply, thenCompose, thenCombine, or any other stage as if no error ever occurred upstream, unless you explicitly throw within handle itself.