Question

Java 8 officially introduce java.io.UncheckedIOException to JDK class libraries for lambda with Stream API, because lambda expressions can't declare its throws-clause and lambda body can't throw checked exception such as IOException.

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

Était-ce utile?

La solution

You would throw it in the same circumstances as the library methods (BufferedReader.lines and Files.lines) that currently do it: that is, when you are wrapping an I/O exception resulting from an operation subsequent to opening a file (file opening operations still throw IOException). As for catching it, that depends on your strategy for IO error recovery: in other words, do whatever you do to handle the wrapped IOException in non-stream code.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top