当前位置: 首页 > 工具软件 > Vavr > 使用案例 >

vavr_使用Vavr在Java 8流中更好的异常处理

岑光熙
2023-12-01

vavr

by Rajasekar Elango

由Rajasekar Elango

In this post, I will provide tips for better exception handling in Java 8 streams using the Functional Java library Vavr.

在这篇文章中,我将提供使用Functional Java库Vavr在Java 8流中更好地处理异常的技巧。

问题 (The problem)

To illustrate with an example, let’s say we want to print the day of the week for a given stream of date strings in the format MM/dd/YYYY.

为了举例说明,假设我们要以MM/dd/YYYY格式打印给定日期字符串流的星期几。

初始解决方案 (Initial solution)

Let’s start with an initial solution, as seen below, and iteratively improve on it.

让我们从一个初始解决方案开始,如下所示,然后对其进行迭代改进。

This will output

这将输出

Huh, if a date string is invalid, this fails at the first DateTimeParseException without continuing with valid dates.

呵呵 ,如果日期字符串是无效的,这未能在第一DateTimeParseException没有与有效日期继续。

Java 8抢救之选 (Java 8 Optional to the rescue)

We can refactor parseDate to return Optional<LocalDate> to make it discard invalids and continue processing valid dates.

我们可以重构parseDate以返回Optional<LocalDa te>,以使其放弃无效值并继续处理有效日期。

This will skip errors and converts all the valid dates.

这将跳过错误并转换所有有效日期。

WEDNESDAY Text '01-01-2015' could not be parsed at index 2 THURSDAY Text 'not a date' could not be parsed at index 0 FRIDAY

This is a great improvement, but the exception has to be handled within the parseDate method and can’t be passed back to the main method to deal with it. We can’t make the parseDate method throw a checked exception as the Streams API doesn’t play well with methods that throw exceptions.

这是一个很大的改进,但是必须在parseDate方法中处理异常,并且不能将其传递回main方法进行处理。 我们不能使parseDate方法抛出检查异常,因为Streams API与抛出异常的方法不能很好地配合使用。

Vavr的Try Monad更好的解决方案 (A better solution with Vavr’s Try Monad)

Vavr is a functional library for Java 8+. We will use the Try object from Vavr, which can be either an instance of Success or Failure. Basically Try is a monadic container type which represents a computation that may either result in an exception, or return a successfully computed value. Here is the modified code using Try:

Vavr是Java 8+的功能库。 我们将使用Vavr中的Try对象,该对象可以是SuccessFailure的实例。 基本上, Try是一种Monadic容器类型,它表示可能导致异常或返回成功计算值的计算。 这是使用Try修改的代码:

The output is

输出是

WEDNESDAY Failed due to Text '01-01-2015' could not be parsed at index 2 THURSDAYFailed due to Text 'not a date' could not be parsed at index 0 FRIDAY

Now the exception is passed back to main to deal with it. Try also has APIs to implement recovery logic or return a default value in case of error.

现在,异常被传递回main进行处理。 Try还提供API来实现恢复逻辑或在出现错误的情况下返回默认值。

To demonstrate this, let’s say we also want to support MM-dd-YYYY as an alternate string format for dates. The below example shows how we can easily implement recovery logic.

为了说明这一点,假设我们还希望支持MM-dd-YYYY作为日期的备用字符串格式。 以下示例显示了我们如何轻松实现恢复逻辑。

The output shows that now the date 01-01-2015 is also successfully converted.

输出显示,现在日期01-01-2015也已成功转换。

WEDNESDAY THURSDAY THURSDAY Failed due to Text 'not a date' could not be parsed at index 0 FRIDAY

So Try Monad can be used to elegantly deal with exceptions and fail fast on errors.

因此, Try Monad可以用于优雅地处理异常,并在发生错误时快速失败

Update on 12/03/2018:

2018年12月3日更新:

The code examples are updated to use Doculet.

代码示例已更新为使用Doculet

Originally published at http://erajasekar.com/posts/better-exception-handling-java8-streams-using-javaslang/.

最初发布在http://erajasekar.com/posts/better-exception-handling-java8-streams-using-javaslang/中

翻译自: https://www.freecodecamp.org/news/better-exception-handling-in-java-8-streams-using-vavr-6eda31285ce9/

vavr

 类似资料: