JVM Advent

The JVM Programming Advent Calendar

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 Lombok’s @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. Plus, you might be part of a team that banned (!) Lombok. But 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 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 you, then I would advise 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, isn’t it? It’s so good that of course others had 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());

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());

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

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.

Learn More

In case you liked the article, then check out my website (victorrentea.ro) for recorded talks, live and recorded webinars, blog posts and more goodies. If you have any questions or remarks you can also reach out to me on LinkedIN, Twitter, or Facebook.

Next Post

Previous Post

2 Comments

  1. Paolo December 25, 2020

    “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” other than difficult debugging what’s the problem with this kind of high order functions? They open the door to a lot of new opportunities like lazy obj instantiation, more powerful dynamic behaviour and the like. Anyway, returning to the problem of checked exceptions in lambdas, don’t you think that a simple utility function would solve the issue W/O having to introduce yet another dependency aka liability?
    Thanks 🙂

    • Victor Rentea December 25, 2020 — Post Author

      Functions returning functions make the a larfe majority of developers I worked with feel uncomfortable. So, if everyone in the team is super comfy with them, then for sure, use them. But first, please do a little workshop/series of complex exercises first with your team.

      And also, i would avoid avoid mixing such tech complexity with domain logic complexity. So ok to use for a tech geek part of your app, that isn’t very biz logic intensive

      Then, about adding a new library, then if you consider this to be a liability, sure, copy paste the method. But there might be a lot to copy for all the combinations of functions. But my gut feeling is that jool is not an intrusive framework you should be wary of, but indeed one aiming to do a small focused thing: ease the use of lambdas with Java.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 JVM Advent | Powered by steinhauer.software Logosteinhauer.software

Theme by Anders Norén