vavr_使用Vavr在Java 8流中更好的异常处理
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庫(kù)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.
為了舉例說明,假設(shè)我們要以MM/dd/YYYY格式打印給定日期字符串流的星期幾。
初始解決方案 (Initial solution)
Let’s start with an initial solution, as seen below, and iteratively improve on it.
讓我們從一個(gè)初始解決方案開始,如下所示,然后對(duì)其進(jìn)行迭代改進(jìn)。
This will output
這將輸出
Huh, if a date string is invalid, this fails at the first DateTimeParseException without continuing with valid dates.
呵呵 ,如果日期字符串是無(wú)效的,這未能在第一DateTimeParseException沒有與有效日期繼續(xù)。
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.
我們可以重構(gòu)parseDate以返回Optional<LocalDa te>,以使其放棄無(wú)效值并繼續(xù)處理有效日期。
This will skip errors and converts all the valid dates.
這將跳過錯(cuò)誤并轉(zhuǎn)換所有有效日期。
WEDNESDAY Text '01-01-2015' could not be parsed at index 2 THURSDAY Text 'not a date' could not be parsed at index 0 FRIDAYThis 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.
這是一個(gè)很大的改進(jìn),但是必須在parseDate方法中處理異常,并且不能將其傳遞回main方法進(jìn)行處理。 我們不能使parseDate方法拋出檢查異常,因?yàn)镾treams 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+的功能庫(kù)。 我們將使用Vavr中的Try對(duì)象,該對(duì)象可以是Success或Failure的實(shí)例。 基本上, Try是一種Monadic容器類型,它表示可能導(dǎo)致異常或返回成功計(jì)算值的計(jì)算。 這是使用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 FRIDAYNow 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.
現(xiàn)在,異常被傳遞回main進(jìn)行處理。 Try還提供API來(lái)實(shí)現(xiàn)恢復(fù)邏輯或在出現(xiàn)錯(cuò)誤的情況下返回默認(rèn)值。
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.
為了說明這一點(diǎn),假設(shè)我們還希望支持MM-dd-YYYY作為日期的備用字符串格式。 以下示例顯示了我們?nèi)绾屋p松實(shí)現(xiàn)恢復(fù)邏輯。
The output shows that now the date 01-01-2015 is also successfully converted.
輸出顯示,現(xiàn)在日期01-01-2015也已成功轉(zhuǎn)換。
WEDNESDAY THURSDAY THURSDAY Failed due to Text 'not a date' could not be parsed at index 0 FRIDAYSo Try Monad can be used to elegantly deal with exceptions and fail fast on errors.
因此, Try Monad可以用于優(yōu)雅地處理異常,并在發(fā)生錯(cuò)誤時(shí)快速失敗 。
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/.
最初發(fā)布在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
總結(jié)
以上是生活随笔為你收集整理的vavr_使用Vavr在Java 8流中更好的异常处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女人梦到马了什么预兆
- 下一篇: 如何构建自己的免费无服务器评论框