首页 > 解决方案 > 使用 VAVR 惯用地处理异常

问题描述

从我们嘴里吐出 Google Guava kool-aid,并一头扎进我们对 VAVR 及其闪闪发光的理想的新迷恋中,假设我们正在map()ping a StreamfoldLeft()在 a 上执行Traversable,或类似的,并且其中一个内部功能抛出已检查的异常。现在我们正盯着编译器错误,未处理的异常。

使用惯用的 VAVR 如何理想地处理此类异常。生成的代码是什么样的,模式是什么。我们有Options 和Trys... 这一切是如何结合在一起的。

哦,像偷偷摸摸的例外这样的技巧让我们不感兴趣。

标签: javavavr

解决方案


您可以使用CheckedFunction[0-8].liftTry()将抛出检查异常的函数转换为总函数,该函数返回包装在Try. 如果原始函数返回一个值而不抛出,它将被包裹在 a 中Success,如果它抛出,异常将被包裹在 a 中Failure

然后,您需要决定如何在多个值的上下文中处理错误。下面是一些关于你可以用一堆Try值做什么的例子。

Array<String> input = Array.of(
        "123", "456", "789", "not a number", "1111", "another non-number"
);

// try and parse all the strings
Array<Try<Integer>> trys = input.map(CheckedFunction1.liftTry(Integer::parseInt));

// you can take just the successful values
Array<Integer> values = trys.flatMap(Try::iterator);

// you can look just for the failures
Array<Throwable> failures = trys.filter(Try::isFailure).map(Try::getCause);

// you can partition by the outcome and extract values/errors
Tuple2<Traversable<Integer>, Traversable<Throwable>> partition =
    trys.partition(Try::isSuccess)
        .map(
            seq -> seq.map(Try::get),
            seq -> seq.map(Try::getCause)
        );

// you can do a short-circuiting parse of the original sequence
// this will stop at the first error and return it as a failure
// or take all success values and wrap them in a Seq wrapped in a Try
Try<Seq<Integer>> shortCircuit = Try.sequence(
    input.iterator() //iterator is lazy, so it's not fully evaluated if not needed
         .map(CheckedFunction1.liftTry(Integer::parseInt))
);
// Failure(java.lang.NumberFormatException: For input string: "not a number")

当然,您可以使用任何其他 vavr 集合代替Array.


推荐阅读