0

java.util.concurrent.CompletionStage#exceptionally expects a function that returns a value, but what if I just want a Consumer for a Throwable like java.util.concurrent.CompletionStage#thenAccept for successful executions?

EDIT: To be clear, I know about whenComplete but that does not "take a Consumer for a Throwable like java.util.concurrent.CompletionStage#thenAccept".

andresp
  • 1,624
  • 19
  • 31
  • 2
    You could try `whenComplete()` which takes a `BiConsumer` that itself either gets the result or the throwable. – Thomas Nov 18 '21 at 13:55
  • I know about `whenComplete` but that is not what I am asking for. – andresp Nov 18 '21 at 13:57
  • 2
    In that case you might want to elaborate a little why `whenComplete()` doesn't work for you. You asked for a "what if I want to use a consumer" - well, `whenComplete()` might be an option if there's no other (I don't see any because `CompletionStage.thenAcceptXxx()` are the only methods taking a `Consumer`). – Thomas Nov 18 '21 at 14:03
  • I said "what if I just want a consumer for a Throwable like java.util.concurrent.CompletionStage#thenAccept for successful executions". whenComplete doesn't take a consumer for a Throwable. I just want to confirm there is no such a thing in the Java API. I think there is nothing like this, but I can be wrong. – andresp Nov 18 '21 at 14:07
  • 2
    Well, the next best thing to a method taking a `Consumer` is the `BiConsumer` that `whenComplete()` takes and which gets either the result or the throwable (as per implementation of `CompletableFuture.uniWhenComplete()` which calls `accept()`. If you want individual consumers you could create a `BiConsumer` that takes a `Consumer` for each of the parameters and which you call if the parameter is not null. – Thomas Nov 18 '21 at 15:55
  • Thanks, but the intention is really to confirm this doesn't exist in the Java API and I am not missing anything. I am curious to know why this doesn't exist, but that would be a different question. – andresp Nov 18 '21 at 18:30
  • @andresp such a question would probably be considered opinion-based, and thus not a good fit for SO. Indeed you would never have a definitive answer for that, just “_they might not have found it necessary_”, “_they probably didn’t want to clutter an already big API with things you can easily do with existing methods_” or even “_maybe they didn’t think about it_” and such… – Didier L Nov 18 '21 at 23:01
  • @DidierL I think if this was discussed in the Java mailing lists there is a possibility to provide a non opinion based response to that question. There are several examples of such questions in SO, some of them being quite popular eg https://stackoverflow.com/questions/24547673/why-java-util-optional-is-not-serializable-how-to-serialize-the-object-with-suc or https://stackoverflow.com/questions/77718/why-doesnt-java-offer-operator-overloading – andresp Nov 19 '21 at 15:47
  • "The intention is really to confirm this doesn't exist in the Java API and I am not missing anything" - It might have helped if your question already stated so, as you're not looking for an alternative or a solution but rather for a confirmation that what you're looking for doesn't exist. – Thomas Nov 20 '21 at 08:12
  • I am looking for a solution as long as it matches what I am asking for. I have edited the description to make it more clear. – andresp Nov 20 '21 at 15:25

0 Answers0