java - 使用 VAVR 惯用地处理异常
问题描述
从我们嘴里吐出 Google Guava kool-aid,并一头扎进我们对 VAVR 及其闪闪发光的理想的新迷恋中,假设我们正在map()
ping a Stream
,foldLeft()
在 a 上执行Traversable
,或类似的,并且其中一个内部功能抛出已检查的异常。现在我们正盯着编译器错误,未处理的异常。
使用惯用的 VAVR 如何理想地处理此类异常。生成的代码是什么样的,模式是什么。我们有Option
s 和Try
s... 这一切是如何结合在一起的。
哦,像偷偷摸摸的例外这样的技巧让我们不感兴趣。
解决方案
您可以使用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
.
推荐阅读
- javascript - 我可以从 Wordpress 数据库中获取 React 应用程序的用户密码吗?
- c++ - 将 Rcpp 的 as/wrap 函数放在哪里?
- java - JavaFX 预加载器问题
- c# - 在 DataGridView 底部添加行时出现问题
- c - 传递数组名称时 sizeof 如何工作
- java - springboot-aop 无法获取 joinPoint 参数的类对象
- google-api - 带有同义词的谷歌翻译 API
- amazon-web-services - AWS APi Gateway VPC LINK - 确保您传递的是 Gzip 流
- java - 如何在可用的詹金斯奴隶上平均分配测试?
- python - numpy中deg2rad和弧度之间的区别?