What is the idiom / best practice with UncheckedIOException and Stream API?

Java 8 officially introduces java.io.UncheckedIOException into the JDK class libraries for lambdas with the Stream API, because lambdas cannot declare its throws -clause and the lambdas body cannot throw a checked exception like IOException .

What is the idiom / best practice with UncheckedIOException and Stream API? What conditions do I explicitly throw a new UncheckedIOException object, and when should I catch a UncheckedIOException exception?

+6
source share
1 answer

You would throw it in the same circumstances as the library methods ( BufferedReader.lines and Files.lines ) that currently do this: that is, when you complete an I / O exception that results from the operation after opening the file (open operation the file is still throwing an IOException). As for catching, it depends on your strategy for recovering I / O errors: in other words, do whatever you do to handle the IOEx wrapped exception in non-threaded code.

+6
source

All Articles