首页 > 试题广场 >

下列程序的输出结果为( ) public class Bas

[单选题]
下列程序的输出结果为(      )
public class Base {
    public static void main(String[] args) {
        int a = 6;
        int b = 0;

        try {
            System.out.println("start");
            a = a / b;
            System.out.println("try");
        } 
        catch (ArithmeticException e) {
            System.out.println("ArithmeticException");
        } 
        catch (Exception e) {
            System.out.println("Exception");
        }

        System.out.println("end");
    }
}
  • start
    ArithmeticException
  • start
    Exception
    end
  • start
    ArithmeticException
    try
    end
  • start
    ArithmeticException
    end
当执行a/b的时候就会被范围更小且位于最前面的ArithmeticException异常捕获,因此try不会再打印,下面范围更大的Exception块也不会再执行
发表于 2021-09-03 14:13:44 回复(0)
按照捕获顺序,前面的先捕获,后面的在无需进行捕获。
发表于 2021-12-21 15:38:55 回复(0)
异常信息被封装后匹配catch的过程类比于switch中匹配case的过程
发表于 2023-04-04 22:59:34 回复(0)