// try-with-resources: los recursos se cierran siempre, en orden inverso.
public class ResourceDemo {

    public static void main(String[] args) {
        IO.println("-- 1. sin errores");
        try (Gate a = new Gate("A", false); Gate b = new Gate("B", false)) {
            a.use(false);
            b.use(false);
        }

        IO.println("-- 2. el bloque lanza");
        try (Gate a = new Gate("A", false); Gate b = new Gate("B", false)) {
            a.use(false);
            b.use(true);
            IO.println("no llega aquí");
        } catch (IllegalStateException e) {
            IO.println("capturada: " + e.getMessage());
        }

        IO.println("-- 3. el bloque lanza y close también");
        try (Gate a = new Gate("A", true)) {
            a.use(true);
        } catch (IllegalStateException e) {
            IO.println("capturada: " + e.getMessage());
            for (Throwable s : e.getSuppressed()) {
                IO.println("suprimida: " + s.getMessage());
            }
        }

        IO.println("-- 4. finally");
        try {
            IO.println("en el try");
            return;
        } finally {
            IO.println("finally se ejecuta aunque haya return");
        }
    }
}
