Skip to content

Ignore exception in stream operations

Assuming you have an exception (checked/unchecked) in a stream operation and you want to ignore from now on this element. The stream must not be aborted, just ignoring elements throwing exceptions. I explicitly avoid saying skip, because it is a stream operation.

So the example is using the map() operation for demonstration. Here I have a division by zero (for example), so the “map” should skip this element.

As an example:

public void ignoreException() {
    assertThat(Stream.of(1,2,1,3).map(i -> 10 / i).reduce(0, Integer::sum), is(28));
    // the zero will break the next stream
    assertThat(Stream.of(1,2,0,3).map(i -> 10 / i).reduce(0, Integer::sum), is(18));

So the division by zero can break the whole stream.

I found a lot of articles that wrap a runtime exception in a checked exception (throw new RuntimeException(ex)). Or partial vs. total functions.

Or I made a wrapper returning a java.util.function.Function (e.g: -> 10/i))...), returning a “null” in the case of a exception. But right-hand operation may now fail, as in my example (reduce).

The only useful approach is an “EITHER” concept (a stream of EITHER), so the division by zero in my example will become a “left” and can be handled in a different way.


There are relatively few operations on streams that can achieve a transformation of elements and result in elements being dropped — in fact, there’s really only one, flatMap.

So your wrapper more or less has to look like

interface CanThrow<F, T> { T apply(F from) throws Exception; }
<T, R> Function<T, Stream<R>> wrapper(CanThrow<T, R> fn) {
  return t -> {
   try {
    return Stream.of(fn.apply(t));
   } catch (Exception ignored) { return Stream.empty(); }

assertThat(Stream.of(1, 2, 0, 3).flatMap(wrapper(i -> 10 / i)).reduce(0, Integer::sum))