// 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) 
import java.util.Random;

public class ExceptionFun {
    private static Random rng = new Random();
    @FunctionalInterface
    public static interface Test<T extends Exception> {
        public void dummy() throws T;
    }
    public static class Exception0 extends Exception { }
    public static class Exception1 extends Exception { }
    public static class Exception2 extends Exception { }
    public static <U extends Exception> void demo(Test<U> exp) throws Exception0, U {
        if(rng.nextDouble() < 0.01) throw new Exception0();
        exp.dummy();
    }
    public static void main(String[] args) throws Exception0, Exception1, Exception2 {
        demo(() -> { System.out.println("A");});
        demo(() -> { if(rng.nextDouble() < 0.01) throw new Exception1(); System.out.println("B");});
        demo(() -> { if(rng.nextDouble() < 0.01) throw new Exception2(); System.out.println("C");});
    }
}
