【问题标题】:RuntimeException treated as ServerSOAPFaultExceptionRuntimeException 被视为 ServerSOAPFaultException
【发布时间】:2019-05-01 12:02:45
【问题描述】:

为什么要如下代码:

/.../
.onFailure(exc ->
                        Match(exc).of(
                                Case($(instanceOf(ServerSOAPFaultException.class)), handleServerSOAPFaultException(exc)),
                                Case($(instanceOf(Exception.class)), handleDefaultException(exc))
                        ))
                .getOrElseGet(exc -> false);

当异常为 RuntimeException 时调用第一种情况。 RuntimeException 不是 ServerSOAPFaultException 的实例。

【问题讨论】:

    标签: java exception vavr


    【解决方案1】:

    这是一个 Java 问题,而不是特定于 Vavr 的问题:在 Java 中,如果您编写 foo(a, b),那么 ab 将在 foo(a, b) 被评估之前被评估(称为“渴望”)。

    所以要评估Match().of(),它必须评估所有Case()。要评估每个Case(),它必须评估handleServerSOAPFaultException(exc)

    如果您只想在大小写匹配时评估handleServerSOAPFaultException(exc),则需要将其设为惰性调用,即函数,即Supplier

    /.../
    .onFailure(exc ->
                            Match(exc).of(
                                    Case($(instanceOf(ServerSOAPFaultException.class)), () -> handleServerSOAPFaultException(exc)),
                                    Case($(instanceOf(Exception.class)), () -> handleDefaultException(exc))
                            ))
                    .getOrElseGet(exc -> false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-03
      • 2019-01-15
      • 2016-11-18
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多