Java8 - Exception handling in CompletableFuture

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 07:55:31 Viewed : 250


Exception handling in CompletableFuture involves mechanisms to deal with exceptions that may occur during the asynchronous computation. You can handle exceptions using methods like exceptionally, handle, and whenComplete. Lets explore each of these with examples and outputs:

1. exceptionally:

The exceptionally method is used to handle exceptions that occur during the computation of a CompletableFuture. It allows you to provide a fallback value if an exception occurs.

java
import java.util.concurrent.CompletableFuture; public class CompletableFutureExceptionHandlingExample { public static void main(String[] args) throws Exception { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { throw new RuntimeException("Exception occurred"); }); // Handling exception using exceptionally CompletableFuture<String> exceptionHandledFuture = future.exceptionally(ex -> "Handled Exception: " + ex.getMessage()); // Waiting for completion and printing the result System.out.println("Result: " + exceptionHandledFuture.join()); } }

Output:

php
Result: Handled Exception: java.lang.RuntimeException: Exception occurred

2. handle:

The handle method allows you to handle both the result and any exception that occurred during the computation. It is useful for scenarios where you want to perform some logic regardless of whether an exception occurred or not.

java
import java.util.concurrent.CompletableFuture; public class CompletableFutureHandleExample { public static void main(String[] args) throws Exception { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { throw new RuntimeException("Exception occurred"); }); // Handling both result and exception using handle CompletableFuture<String> handledFuture = future.handle((result, ex) -> { if (ex != null) { return "Handled Exception: " + ex.getMessage(); } else { return "Result: " + result; } }); // Waiting for completion and printing the result System.out.println(handledFuture.join()); } }

Output:

php
Handled Exception: java.lang.RuntimeException: Exception occurred

3. whenComplete:

The whenComplete method is used for asynchronous callbacks that are executed when the CompletableFuture completes, either successfully or with an exception. It does not affect the result or exception.

java
import java.util.concurrent.CompletableFuture; public class CompletableFutureWhenCompleteExample { public static void main(String[] args) throws Exception { CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { throw new RuntimeException("Exception occurred"); }); // Asynchronous callback using whenComplete CompletableFuture<Void> callbackFuture = future.whenComplete((result, ex) -> { if (ex != null) { System.out.println("Exception occurred: " + ex.getMessage()); } else { System.out.println("Result: " + result); } }); // Waiting for completion callbackFuture.join(); } }

Output:

php
Exception occurred: java.lang.RuntimeException: Exception occurred

These examples demonstrate various ways to handle exceptions in CompletableFuture. Depending on your use case, you can choose the method that fits your error-handling strategy.

Search
Related Articles

Leave a Comment: