I have a method that deletes some files:
void deepDelete(Path root) {
Files.walk(root)
.filter(p -> !Files.isDirectory(p))
.forEach(p -> { try { Files.delete(p); }
catch (IOException e) { }
});
}
The try / catch block reduces the readability of the operation, especially against the use of a method reference:
void deepDelete(Path root) throws IOException {
Files.walk(root)
.filter(p -> !Files.isDirectory(p))
.forEach(Files::delete);
}
Unfortunately, this code does not compile.
Is there a way to apply an action that throws checked exceptions in a terminal operation and simply "reconstructs" any exceptions?
I understand that I can write a wrapper that converts a checked exception to an exception, but I prefer sticking to methods in the JDK if possible.
source
share