Let's say I have a method which throws a runtime exception. I'm using a Stream
to call this method on items in a list.
class ABC {
public void doStuff(MyObject myObj) {
if (...) {
throw new IllegalStateException("Fire! Fear! Foes! Awake!");
}
// do stuff...
}
public void doStuffOnList(List<MyObject> myObjs) {
try {
myObjs.stream().forEach(ABC:doStuff);
} catch(AggregateRuntimeException??? are) {
...
}
}
}
Now I want all items in the list to be processed, and any runtime exceptions on individual items to be collected into an "aggregate" runtime exception which will be thrown at the end.
In my real code, I am making 3rd party API calls which may throw runtime exceptions. I want to make sure that all items are processed and any errors reported at the end.
I can think of a few ways to hack this out, such as a map()
function which catches and returns the exception (..shudder..). But is there a native way to do this? If not, is there another way to implement it cleanly?
Aggregate operations − Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on. Pipelining − Most of the stream operations return stream itself so that their result can be pipelined.
The most straightforward way would be to use a try-catch block, wrap the checked exception into an unchecked exception and rethrow it: List<Integer> integers = Arrays. asList(3, 9, 7, 0, 10, 20); integers. forEach(i -> { try { writeToFile(i); } catch (IOException e) { throw new RuntimeException(e); } });
Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.
In this simple case where the doStuff
method is void
and you only care about the exceptions, you can keep things simple:
myObjs.stream() .flatMap(o -> { try { ABC.doStuff(o); return null; } catch (RuntimeException ex) { return Stream.of(ex); } }) // now a stream of thrown exceptions. // can collect them to list or reduce into one exception .reduce((ex1, ex2) -> { ex1.addSuppressed(ex2); return ex1; }).ifPresent(ex -> { throw ex; });
However, if your requirements are more complicated and you prefer to stick with the standard library, CompletableFuture
can serve to represent "either success or failure" (albeit with some warts):
public static void doStuffOnList(List<MyObject> myObjs) { myObjs.stream() .flatMap(o -> completedFuture(o) .thenAccept(ABC::doStuff) .handle((x, ex) -> ex != null ? Stream.of(ex) : null) .join() ).reduce((ex1, ex2) -> { ex1.addSuppressed(ex2); return ex1; }).ifPresent(ex -> { throw new RuntimeException(ex); }); }
There are already some implementations of Try
monad for Java. I found better-java8-monads library, for example. Using it, you can write in the following style.
Suppose you want to map your values and track all the exceptions:
public String doStuff(String s) { if(s.startsWith("a")) { throw new IllegalArgumentException("Incorrect string: "+s); } return s.trim(); }
Let's have some input:
List<String> input = Arrays.asList("aaa", "b", "abc ", " qqq ");
Now we can map them to successful tries and pass to your method, then collect successfully handled data and failures separately:
Map<Boolean, List<Try<String>>> result = input.stream() .map(Try::successful).map(t -> t.map(this::doStuff)) .collect(Collectors.partitioningBy(Try::isSuccess));
After that you can process successful entries:
System.out.println(result.get(true).stream() .map(t -> t.orElse(null)).collect(Collectors.joining(",")));
And do something with all the exceptions:
result.get(false).stream().forEach(t -> t.onFailure(System.out::println));
The output is:
b,qqq java.lang.IllegalArgumentException: Incorrect string: aaa java.lang.IllegalArgumentException: Incorrect string: abc
I personally don't like how this library is designed, but probably it will be suitable for you.
Here's a gist with complete example.
Here's a variation on the theme of mapping-to-exceptions.
Start with your existing doStuff
method. Note that this conforms to the functional interface Consumer<MyObject>
.
public void doStuff(MyObject myObj) {
if (...) {
throw new IllegalStateException("Fire! Fear! Foes! Awake!");
}
// do stuff...
}
Now write a higher-order function that wraps this and turns this into a function that might or might not return an exception. We want to call this from flatMap
, so the way "might or might not" is expressed is by returning a stream containing the exception or an empty stream. I'll use RuntimeException
as the exception type here, but of course it could be anything. (In fact it might be useful to use this technique with checked exceptions.)
<T> Function<T,Stream<RuntimeException>> ex(Consumer<T> cons) {
return t -> {
try {
cons.accept(t);
return Stream.empty();
} catch (RuntimeException re) {
return Stream.of(re);
}
};
}
Now rewrite doStuffOnList
to use this within a stream:
void doStuffOnList(List<MyObject> myObjs) {
List<RuntimeException> exs =
myObjs.stream()
.flatMap(ex(this::doStuff))
.collect(Collectors.toList());
System.out.println("Exceptions: " + exs);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With