I have the code below.
private static void readStreamWithjava8() {
Stream<String> lines = null;
try {
lines = Files.lines(Paths.get("b.txt"), StandardCharsets.UTF_8);
lines.forEachOrdered(line -> process(line));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (lines != null) {
lines.close();
}
}
}
private static void process(String line) throws MyException {
// Some process here throws the MyException
}
Here my process(String line)
method throws the checked exception and I'm calling that method from within the lambda. At that point need to throw the MyException
from readStreamWithjava8()
method without throwing RuntimeException
.
How can I do this with java8?