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:
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.
javaimport 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:
phpResult: Handled Exception: java.lang.RuntimeException: Exception occurred
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.
javaimport 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:
phpHandled Exception: java.lang.RuntimeException: Exception occurred
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.
javaimport 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:
phpException 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.