Functional Java - whenComplete和异常之间的交互 [英] Functional Java - Interaction between whenComplete and exceptionally

查看:1925
本文介绍了Functional Java - whenComplete和异常之间的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码中:

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

doSomethingThatMightThrowAnException 出现异常时,都是 doSomethingElse handleException 运行,或者是 whenComplete 异常



编辑:


$ b $ doSomethingThatMightThrowAnException 返回一个 CompletableFuture ,它可能 completeExceptionally 。这是我所说的例外。 whenComplete $ b


返回一个新的CompletionStage ,其结果或例外与本阶段相同,这个阶段完成后执行给定的动作。


(强调我的)



这意味着这个阶段不会吞噬异常,因为它应该具有相同的结果或例外。但是,您可能会对以下事实感到意外:后续阶段会收到包装在 CompletionException 中的前一个阶段的异常,如前所述 here ,所以它不是完全一样的异常:

  CompletableFuture<字符串> test = new CompletableFuture<>(); 
test.whenComplete((result,ex) - > System.out.println(stage 2:+ result +\t+ ex))
.exceptionally(ex - > { System.out.println(stage 3:+ ex); return;});
test.completeExceptionally(new IOException());

将会列印:

 第2阶段:null java.io.IOException 
阶段3:java.util.concurrent.CompletionException:java.io.IOException

$ p
$ b

请注意,您可以在一个阶段添加多个操作,而不是链接:

  CompletableFuture<字符串> test = new CompletableFuture<>(); 
test.whenComplete((result,ex) - > System.out.println(stage 2a:+ result +\t+ ex));
test.exceptionally(ex - > {System.out.println(stage 2b:+ ex); return;});
test.completeExceptionally(new IOException());



  stage 2b:java。 io.IOException 
stage 2a:null java.io.IOException

当然,从现在开始阶段 2a 2b 之间没有依赖关系,它们之间没有排序,并且在异步操作的情况下,他们可能会同时运行。


In this code:

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

When there is an exception from doSomethingThatMightThrowAnException, are both doSomethingElse and handleException run, or is the exception consumed by either the whenComplete or the exceptionally?

EDIT:

doSomethingThatMightThrowAnException returns a CompletableFuture, which might completeExceptionally. This is the exception I'm talking about.

解决方案

The documentation of whenComplete says:

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action when this stage completes.

(emphasis mine)

This implies that an exception is not swallowed by this stage as it is supposed to have the same result or exception. However, you might be surprised by the fact that subsequent stages will receive the exception of a previous stage wrapped within a CompletionException, as discussed here, so it’s not exactly the same exception:

CompletableFuture<String> test=new CompletableFuture<>();
test.whenComplete((result, ex) -> System.out.println("stage 2: "+result+"\t"+ex))
    .exceptionally(ex -> { System.out.println("stage 3: "+ex); return ""; });
test.completeExceptionally(new IOException());

will print:

stage 2: null   java.io.IOException
stage 3: java.util.concurrent.CompletionException: java.io.IOException

Note that you can always append multiple actions on one stage instead of chaining then:

CompletableFuture<String> test=new CompletableFuture<>();
test.whenComplete((result, ex) -> System.out.println("stage 2a: "+result+"\t"+ex));
test.exceptionally(ex -> { System.out.println("stage 2b: "+ex); return ""; });
test.completeExceptionally(new IOException());

stage 2b: java.io.IOException
stage 2a: null  java.io.IOException

Of course, since now there is no dependency between the stage 2a and 2b, there is no ordering between them and in the case of async action, they may run concurrently.

这篇关于Functional Java - whenComplete和异常之间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆