1 year ago

#382771

test-img

Zumrud Isgandarli

Why finally block does not execute?

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");

}

java

try-catch

block

try-catch-finally

finally

0 Answers

Your Answer

Accepted video resources