Why finally block does not execute?

59 Views Asked by At

When I run this code, finally block does not execute. If I change System.out to System.err in try block, it works. Or when I change out to err in finally block it works. What is the reason?

Thanks in advance for answer!

    String fn = "data.txt";

try (var w = new BufferedWriter(new FileWriter(fn)); var s = System.out) {

    w.write("Hi, there!");

    w.flush();

    w.write('!');

    System.out.print("1");

} catch (IOException e) {

    System.out.print("2");

} finally {

    System.out.print("3");

}
0

There are 0 best solutions below