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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top