Exceptions and Streams
Tags: java, functional, cleancode
Published: 2021-01-05 08:45:11

*About the Author: Victor is a Java Champion and an experienced Independent Trainer, Speaker, and founder of a huge developer community. More on [victorrentea.ro](https://victorrentea.ro)*

*This article was first published at [Victor's blog](https://victorrentea.ro/blog/exceptions-and-streams/)*

Java 8 gave us `Optional`, a mighty weapon against the most frequent Exception in Java: `NullPointerException`. Unfortunately, Java 8 also brought new headaches regarding exceptions, as the default functional interfaces in Java 8 don’t declare throwing any checked exceptions. So every time you get a checked exception within a lambda, you have to fix that somehow.

## Converting Checked into Runtime Exceptions
The default suggestion offered by most IDEs to auto-fix this issue will produce code like this:
```
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
List<String> dateList = asList("2020-10-11", "2020-nov-12", "2020-12-01");
List<Date> dates = dateList.stream().map(s -> {
   try {
      return format.parse(s);
   } catch (ParseException e) {
      throw new RuntimeException(e);
   }
}).collect(toList());
```

Horrible code.

We could create a dedicated function doing just `.parse` and then cast a spell on it with `@SneakyThrows`, as we've discussed in [a previous article](hide-checked-exceptions-with-sneakythrows):
```
   List<Date> dates = dateList.stream()
		.map(s -> uglyParse(format, s))
		.collect(toList());
   ...
}

@SneakyThrows
private static Date uglyParse(SimpleDateFormat format, String s) {
   return format.parse(s);
}
```
But creating this new method just to hack it with Lombok feels wrong. Indeed, we created it for a purely technical reason: to hide the annoying checked exception which doesn't fit with the `java.util.Function` interface, which doesn't declare to throw anything.

Let's play a bit and create a `ThrowingFunction` interface declaring to throw any checked exception:
```
interface ThrowingFunction<T,R> {
    R apply(T t) throws Exception;
}
```

Then, our `s->format.parse(s)` expression could be [target-typed](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#target-typing) to this new interface, so the following line compiles:

```
ThrowingFunction<String, Date> p = s -> format.parse(s);
// or
ThrowingFunction<String, Date> p = format::parse;
```
Unfortunately, the `Stream.map()` operation takes a `java.util.Function`, you can't change that. But let's imagine we had a function that would take a `ThrowingFunction` and return back a 'classic' `Function`  that doesn't throw any checked exception anymore.

```
Function<String, Date> f = wrapAsRuntime(p);
List<Date> dates = dateList.stream().map(f).collect(toList());
```

And here's the strange `wrapAsRuntime` function:
```
private static <T,R> Function<T, R> wrapAsRuntime(ThrowingFunction<T, R> p) {
	return t -> {
		try {
			return p.apply(t);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	};
}
```
If that's complete nonsense for your, then I would advice that you try to type it yourself. It helps a lot!

Notice that we’ve used generics to make it highly reusable. That's quite a good idea, isnt'it? It's so good that of course others had it it many years ago... :) 

Introducing the `Unchecked.function()` from the [jool library](https://github.com/jOOQ/jOOL) that does EXACTLY what we did above. Using it, the final code looks like:
```
List<Date> dates = dateList.stream().map(Unchecked.function(format::parse)).collect(toList());
```

If you've been using Java 8 for many years, then this library is a must-have.

> **Best-practice**: Whenever checked exceptions are annoying you in lambdas `->` or method references `::`, use `Unchecked.*` to rethrow it as a `RuntimeException`

This doesn't involve any hack in the bytecode ([as @SneakyThrows does](hide-checked-exceptions-with-sneakythrows)), but only plain java code. Passing a function as a parameter to another function is a very useful practice that I will be blogging about soon, but functions that both take and return functions - those I don't like. It's one of the most complex, hard to read, and especially hard to debug in Java. But since it's a library doing it, and the purpose is obvious, I never hesitated to use it many of my projects.

Now let's shift a bit the perspective. No matter how you twist it, the processing of the entire stream stops when the first exception is thrown. But what if we don't want to crash but instead collect all the errors.

## The Try Monad
Let’s change the requirements a bit: we now want to parse all the **valid** dates and return them IF at least half of them are parseable, otherwise we should throw an exception. This time we can’t let an exception terminate the execution of our stream. Instead, we want to go through all of the items and collect both parsed dates and exceptions. For example, if we are given 3 correctly-formatted dates and 2 invalid ones, we should return the 3 ones that we were able to parse correctly.

> Whenever you want to **collect the exceptions** happening in items, consider using the vavr `Try` monad.

The `Try<>` class from the [vavr library](https://www.vavr.io/) is a specialization of the `Either<>` concept present in many functional programming languages. An instance can store either the result or the occurred exception (if any).

```
List<Try<Date>> tries = dateList.stream()
	.map(s -> Try.of(
		() -> format.parse(s) // throwing code
	))
	.collect(toList());
```

If the *throwing code* crashes with an exception, the surrounding `Try.of` function will catch that exception and return a **failed Try**. Therefore, in the `tries` list above, there can be items with `isSuccess()` either `true` or `false`. To count the success ratio, the shortest (geekest) form is:

```
double successRatio = tries.stream()
	.mapToInt(t -> t.isSuccess() ? 1 : 0)
	.average()
	.orElse(0);
```

Then,

```
if (successRatio > .5) {
	return tries.stream()
		.filter(Try::isSuccess)
		.map(Try::get)
		.collect(toList());
} else {
	throw new IllegalArgumentException("Too many invalid dates");
}
```

Problem solved.

To better understand the code, we can extract a function from it, that returns a `Try<>`:

```
private static Try<Date> tryParse(SimpleDateFormat format, String s) {
	return Try.of(() -> format.parse(s));
}
```
This resembles the style of handling exceptions in other languages like Go and Haskell, which return the exception to their callers.

By the way, if you think a bit, you could solve the problem without the `Try`, by sweeping the data twice: first to count the parseable dates, and then to actually parse them. Or even a single pass using a combination of a `.map` returning a `null`/`Optional.empty` for errors, followed by a `.filter`. That could work too, but the `Try` approach might be more readable.

> **Tip**: Consider `*vavr.Try<>` when you want to collect both results and exceptions in a single pass through data.

By the way, if you keep thinking at the "Monad" word, here’s a nice article to get you past that: [Monads for Java developers](https://dzone.com/articles/functor-and-monad-examples-in-plain-java)

**Disclaimer**: avoid streaming a large number of items in batch processing. Instead, stick with the industry default: process the data in chunks, and consider introducing Spring Batch for state-of-the-art batches.


## Conclusions
- Checked exceptions don't play nice with the Java Stream API.
- Use `@SneakyThrows` ([Lombok](https://projectlombok.org/features/SneakyThrows)) or `Unchecked` ([jOOL](http://www.jooq.org/products/jOO%CE%BB/javadoc/0.9.5/org/jooq/lambda/Unchecked.html)) to get rid of checked exceptions with Streams
- Consider `Try` ([vavr](https://www.javadoc.io/doc/io.vavr/vavr/0.10.0/io/vavr/control/Try.html)) whenever you want to collect the errors occurring for an element instead of terminating the Stream.