Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's idiom/best practice with UncheckedIOException and Stream API?

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?

like image 224
yohjp Avatar asked May 01 '14 04:05

yohjp


1 Answers

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.

like image 133
Maurice Naftalin Avatar answered Oct 06 '22 00:10

Maurice Naftalin