try-with-resources: close order
Predict the outputHardJava only
class Res implements AutoCloseable {
private final String name;
Res(String name) {
this.name = name;
System.out.println("open " + name);
}
@Override
public void close() {
System.out.println("close " + name);
}
}
public class Main {
public static void main(String[] args) {
try (Res a = new Res("A"); Res b = new Res("B")) {
System.out.println("body");
throw new RuntimeException("fail");
} catch (RuntimeException e) {
System.out.println("catch " + e.getMessage());
} finally {
System.out.println("finally");
}
}
}
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.