Java's CompletableFuture : time of ExecutionException

198 Views Asked by At

When working with CompletableFuture any potential errors from computing the value are exposed via the get() method throwing an ExecutionException. I can of course log this exception at the time of calling this method, but it would be misleading as this exception may have happened at a completely different time from the current.

In short, I would like information on when the ExecutionException occurred. Is there any elegant solution to this ?

2

There are 2 best solutions below

0
KavboiHaggis On

Try to wrap execution code into try-catch, which you passed to CompletableFuture.

0
KavboiHaggis On

Use callback methods, i.e. exceptionally, whenComplete or handle, which pass a Throwable and a result.