문제

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