50

I have encountered strange behavior of Java 8 CompletableFuture.exceptionally method. If I execute this code, it works fine and prints java.lang.RuntimeException

CompletableFuture<String> future = new CompletableFuture<>();

future.completeExceptionally(new RuntimeException());

future.exceptionally(e -> {
            System.out.println(e.getClass());
            return null;
});

But if I add another step in the future processing like thenApply, the exception type changes to java.util.concurrent.CompletionException with the original exception wrapped inside.

CompletableFuture<String> future = new CompletableFuture<>();

future.completeExceptionally(new RuntimeException());

future.thenApply(v-> v).exceptionally(e -> {
            System.out.println(e);
            return null;
});

Is there any reason why this should be happening? In my opinion, it's quite surprising.

Hearen
  • 7,420
  • 4
  • 53
  • 63
Lukas
  • 13,606
  • 9
  • 31
  • 40
  • 3
    See https://stackoverflow.com/q/49230980/14731 for an overview of when the exception is wrapped (or not). – Gili Jun 06 '18 at 17:24
  • 1
    Here's an article that I found that helped me understand the solutions to this problem, better -> http://millross-consultants.com/completable-future-error-propagation.html – The 0bserver Feb 12 '19 at 08:57
  • @The0bserver, The link you have added is quite informative and clear lots of my doubt though not all :) – NIGAGA Aug 22 '19 at 14:29
  • Glad I could be of some help @NIGAGA . – The 0bserver Aug 27 '19 at 13:11
  • For anyone interested [here is an archived version](https://web.archive.org/web/20190929235406/http://millross-consultants.com/completable-future-error-propagation.html) of the above-mentioned article. – vlp Feb 03 '23 at 12:35

2 Answers2

36

This behavior is specified in the class documentation of CompletionStage (fourth bullet):

Method handle additionally allows the stage to compute a replacement result that may enable further processing by other dependent stages. In all other cases, if a stage's computation terminates abruptly with an (unchecked) exception or error, then all dependent stages requiring its completion complete exceptionally as well, with a CompletionException holding the exception as its cause.

It’s not that surprising if you consider that you may want to know whether the stage you have invoked exceptionally on failed, or one of its direct or indirect prerequisites.

Oliv
  • 10,221
  • 3
  • 55
  • 76
Holger
  • 285,553
  • 42
  • 434
  • 765
6

yes, the behavior is expected, but if you want the original exception which was thrown from one of the previous stages, you can simply use this

CompletableFuture<String> future = new CompletableFuture<>();

future.completeExceptionally(new RuntimeException());

future.thenApply(v-> v).exceptionally(e -> {
        System.out.println(e.getCause()); // returns a throwable back
        return null;
});
Hearen
  • 7,420
  • 4
  • 53
  • 63
Gagandeep Kalra
  • 1,034
  • 14
  • 13