Functional Java - Interaction between whenComplete and exceptionally

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

In this code:

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


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 19:52

    doSomethingThatMightThrowAnException() is chained with .whenComplete((result, ex) -> doSomethingElse()}) and .exceptionally(ex -> handleException(ex)); but if it throws an exception it ends right there as no object will be passed on in the chain.

    Remember that an exception will throw out to the caller, so unless doSomethingThatMightThrowAnException() catches the exception internally it will throw out. If this is your class you should know if it does throw, if not check docs for libraries that you use.

提交回复
热议问题