As I try to understand the exceptionally
functionality, I read several blogs and posts , but I don't understand what is wrong with this code:
public CompletableFuture<String> divideByZero(){
int x = 5 / 0;
return CompletableFuture.completedFuture("hi there");
}
I thought that I will be able to catch the exception when calling the divideByZero
method with the exceptionally
or the handle
, but the program simply prints the stack trace and exits.
I tried both or handle
& exceptionally
:
divideByZero()
.thenAccept(x -> System.out.println(x))
.handle((result, ex) -> {
if (null != ex) {
ex.printStackTrace();
return "excepion";
} else {
System.out.println("OK");
return result;
}
})
But the result is always:
Exception in thread "main" java.lang.ArithmeticException: / by zero