Functional Java - Interaction between whenComplete and exceptionally

后端 未结 3 759
悲&欢浪女
悲&欢浪女 2021-02-01 19:33

In this code:

doSomethingThatMightThrowAnException()
  .whenComplete((result, ex) -> doSomethingElse()})
  .exceptionally(ex -> handleException(ex));
         


        
3条回答
  •  情歌与酒
    2021-02-01 19:41

    The exceptionally method states:

    Returns a new CompletableFuture that is completed when this CompletableFuture completes, with the result of the given function of the exception triggering this CompletableFuture's completion when it completes exceptionally; otherwise, if this CompletableFuture completes normally, then the returned CompletableFuture also completes normally with the same value. Note: More flexible versions of this functionality are available using methods whenComplete and handle.

    This is not, IMHO written in the clearest english but I would say that means that if an exception is thrown then only the exceptionally action will be triggered. If no exception is thrown then only the normal action will be performed.

提交回复
热议问题