Exceptions and Streams

Java 8 gave us Optional, a mighty weapon against the most frequent Exception in Java: NullPointerException, that I talked about in this blog post. However, Java 8 also brought new headaches regarding exceptions, as the default functional interfaces in Java 8 don’t declare throwing any checked exceptions. So usually when 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:

   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 that doesn’t declare throwing 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.Function. You can’t change that, but let’s imagine we had a function that would take a ThrowingFunction and return back a ‘classic’ java.util.function.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 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 – I found that the vast majority of developers have a hard time reasoning about them. It’s one of the most complex, hard to read, and especially hard to debug in Java, so avoid mixing it with complex business logic. But since it’s a library doing it, and the purpose is straightforward, I never hesitated to use it many of my projects. On the other hand, if you want to avoid depending on this library, you just saw the necessary code to write.

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.

Update

The Vavr library also has an equivalent for wrapping checked exceptions into runtime ones (thanks Babis Routis):

.map(CheckedFunction1.of(s -> uglyParse(format, s).unchecked()))

Popular Posts

8 Comments

  1. Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.|

    1. Please let me know what kind of issue you have encountered. Even though images are not critical for this particular blog post, I will use diagrams at some point in the future, so please help me sort it out. Thanks !

  2. Thank you for the tips.

    On a side note, we’ve just implemented something similar and went even further:

    interface ThrowingFunction {
    R apply(T t) throws E;
    }

    static Function apply(ThrowingFunction f) {
    return t -> {
    try {
    return f.apply(t);
    } catch (Exception e) {
    throw new RuntimeException((E) e);
    }
    };
    }

    1. Aren’t some generics missing? somewhere?

      1. Yes they are. For some reason, they were “swallowed” by the blog.

        Let’s give another shot.

        interface ThrowingFunction {
        R apply(T t) throws E;
        }

        static Function apply(ThrowingFunction f) {
        return t -> {
        try {
        return f.apply(t);
        } catch (Exception e) {
        throw new RuntimeException((E) e);
        }
        };
        }

        1. interface ThrowingFunction<T, R, E extends Exception> {
          R apply(T t) throws E;
          }

          static <T, R, E extends Exception> Function<T, R> apply(ThrowingFunction<T, R, E> f) {
          return t -> {
          try {
          return f.apply(t);
          } catch (Exception e) {
          throw new RuntimeException((E) e);
          }
          };
          }

          1. horatiucd says:

            In comments, shall be written as < and > respectively. Sorry about this.

          2. So this allows to selectively catch only a particular exception type. Interesting.
            I feel that this goes beyond the goal of just wrapping the checked into a runtime exception.

Leave a Reply

Your email address will not be published. Required fields are marked *