// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class ErrorHandling {
    public static class ManagedData  {
        public ManagedData() {
            System.out.println("ManagedData memory allocated - GC will deallocate");
        }
    }
    public static class NativeResources implements AutoCloseable {
        public NativeResources() {
            System.out.println("NativeResources allocated");
        }
        @Override
        public void close() {
            System.out.println("NativeResources deallocated");
        }
    }
    public static class MyException extends RuntimeException {
    }
    private static void m3() {
        ManagedData o = new ManagedData();
        System.out.println("**** Error ****");
        throw new MyException();
    }
    private static void m2() {
        try(NativeResources o = new NativeResources()) {
            m3();
            System.out.println("Normal return");
        }
    }
    private static void m1() {
        ManagedData o = new ManagedData();
        m2();
        System.out.println("Normal return");
    }
    public static void main(String[] args) {
        try {
            m1();
            System.out.println("Normal return");
        } catch(MyException ex) {
            System.out.println("GC will deallocate all memory");
        }
    }
}
