易百教程

138、以下 Java 程序的输出是什么?


public class ExceptionHandlingExample {

    public static void main(String args[]) {
        try {
            int a = 1 / 0;
            System.out.println("a = " + a);
        } catch (Exception e) {
            System.out.println(e);
        } catch (ArithmeticException ex) {
            System.out.println(ex);
        }
    }
}

运行结果:

ExceptionHandlingExample.java:10: error: exception ArithmeticException has already been caught
    catch(ArithmeticException ex){System.out.println(ex);}    
    ^
1 error

解释ArithmaticExceptionException 的子类。 因此,不能在 Exception 之后使用。 由于Exception是所有异常的基类,所以最后必须使用它来处理异常。 在此之后不能使用任何类。