public class Solution {
public static void main(String[] args) {
DBConnectionManager dbConnectionManager = new DBConnectionManager();
try (FakeConnection fakeConnection = dbConnectionManager.getFakeConnection()) {
System.out.println("Entering body of try block.");
fakeConnection.usefulOperation();
fakeConnection.unsupportedOperation();
} catch (Exception e) {
}
}
public class FakeConnection implements AutoCloseable {
public FakeConnection() {
System.out.println("Establishing database connection...");
}
public void unsupportedOperation() {
System.out.println("Operation is not supported yet!");
throw new RuntimeException("UnsupportedOperation!");
}
public void usefulOperation() {
System.out.println("Executing useful operation.");
}
@Override
public void close() throws Exception {
System.out.println("Closing database connection...");
}
}
Output is without RuntimeException:
Establishing database connection...
Entering body of try block.
Executing useful operation.
Operation is not supported yet!
Closing database connection...
We have something like this, and I'm curious, because I though that in unsupportedOperation() we trow new RuntimeException() , but it is not what happened.
Why? I don't know how understand teachers acapit from conditions:
"Note that the resources were released automatically,
despite the fact that an exception was thrown by the
unsupportedOperation method.
Requirements:"