DEV Community

Cover image for Exceptions and Streams
Victor Rentea
Victor Rentea

Posted on • Originally published at victorrentea.ro

Exceptions and Streams

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

This article was first published at Victor's blog

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());
Enter fullscreen mode Exit fullscreen mode

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:

   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);
}
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Then, our s->format.parse(s) expression could be target-typed to this new interface, so the following line compiles:

ThrowingFunction<String, Date> p = s -> format.parse(s);
// or
ThrowingFunction<String, Date> p = format::parse;
Enter fullscreen mode Exit fullscreen mode

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());
Enter fullscreen mode Exit fullscreen mode

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);
        }
    };
}
Enter fullscreen mode Exit fullscreen mode

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 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());
Enter fullscreen mode Exit fullscreen mode

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), 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 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());
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Then,

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

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));
}
Enter fullscreen mode Exit fullscreen mode

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

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) or Unchecked (jOOL) to get rid of checked exceptions with Streams
  • Consider Try (vavr) whenever you want to collect the errors occurring for an element instead of terminating the Stream.

Top comments (1)

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Please annotate your code blocks with language to enable syntax highlighting
lang annotation
(like so)