JDK7 的多异常捕获块

JDK7 中包含大量的新特性和代码风格,本代码是介绍在一个异常捕获块中同时捕获多个异常

public class Test{

    public static void main (String args[]){
        try {
            if (args[0].equals("null")) {
                throw (new NullPointerException());
            } else {
                throw (new ArrayIndexOutOfBoundsException());
            }             
        } catch (NullPointerException | ArrayIndexOutOfBoundsException ex) {
            ex.getMessage();
        }
    }
}

编程技巧