3 ms·
Note that try (Stream<String> lines = Files.lines(path)) { ... } has a catch (pun not intended ;)), in that if you write catch (IOExcepti
by layer8 2y ago
Note that
try (Stream<String> lines = Files.lines(path)) {
...
}
has a catch (pun not intended ;)), in that if you write
catch (IOException ex) {
... handle it ...
}
this surprisingly only catches I/O errors thrown directly by the lines() method, but not those thrown during the stream processing, since those are UncheckedIOExceptions. This is because the Stream API designers decided (very unfortunately, IMO) to not want to deal with checked exceptions.
I therefore prefer using Files::newBufferedReader and BufferedReader::readLine.
———
The `new Scanner(path)` example is arguably even worse, because upon I/O error the tokens() implementation simply behaves as if the end of the file was reached, and you have to manually check with Scanner::ioException if an error occurred.