class Owner<T extends AutoCloseable> implements AutoCloseable {
    private T t;

    public Owner(T t) {
        this.t = t;
    }

    @Override
    public void close() throws Exception {
        if (t != null) {
            t.close();
            t = null;
        }
    }

    public T release() {
        T result = t;
        t = null;
        return result;
    }
}

class Bender implements AutoCloseable {
    private final String name;
    private final boolean bad;

    public Bender(String name) throws Exception {
        throw new Exception(name + ": bite my shiny metal ass");
    }

    public Bender(String name, boolean bad) {
        this.name = name;
        this.bad = bad;
    }

    @Override
    public void close() throws Exception {
        if (bad) {
            throw new Exception(name + ": kill all humans");
        } else {
            System.out.println(name + ": all hail to hypnotoad");
        }
    }
}

public class Main implements AutoCloseable {
    private Bender first;
    private Bender second;

    public Main() throws Exception {
        try (Owner<Bender> first = new Owner<Bender>(new Bender("first", false))) {
            second = new Bender("second");
            this.first = first.release();
        }
    }

    public Main(boolean bad) throws Exception {
        try (Owner<Bender> first = new Owner<Bender>(new Bender("first", false))) {
            second = new Bender("second", bad);
            this.first = first.release();
        }
    }

    @Override
    public void close() throws Exception {
        try (Bender first = this.first; Bender second = this.second) {
        }
    }

    public static void main(String[] args) {
        try (Main twr = new Main()) {
            System.out.println("first test");
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        System.out.println();

        try (Main twr = new Main(true)) {
            System.out.println("second test");
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        System.out.println();

        try (Main twr = new Main(false)) {
            System.out.println("third test");
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        System.out.println();
    }
}
