Monday, June 27, 2011

Multi catch in Exceptions


The multi-catch is very useful indeed and significantly reduces the number of lines of code to do such things from before.


public class MultiCatchException {

    static class Exception1 extends Exception {}
    static class Exception2 extends Exception {}

    public static void main(String[] args) {
        try {
            boolean test = true;
            if (test) {
                throw new Exception1();
            } else {
                throw new Exception2();
            }
        } catch (Exception1 | Exception2 e) {
        }
    }

}

No comments:

Post a Comment

Chitika