Skip to content

Instantly share code, notes, and snippets.

@jasmine-k
Created March 26, 2018 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasmine-k/449e4f44bf54a16ce18d2acbe228a64a to your computer and use it in GitHub Desktop.
Save jasmine-k/449e4f44bf54a16ce18d2acbe228a64a to your computer and use it in GitHub Desktop.
private static void exceptionUsingHandle() {
Integer age = -1;
CompletableFuture<String> exceptionFuture = CompletableFuture.supplyAsync(() -> {
if (age < 0) {
throw new IllegalArgumentException("Age can not be negative");
}
if (age > 18) {
return "Adult";
} else {
return "Child";
}
}).handle((result, ex) -> {
if (ex != null) {
System.out.println("Oops! We have an exception - " + ex.getMessage());
return "Unknown!";
}
return result;
});
exceptionFuture.thenAccept(System.out::println); // Unknown!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment