- class Demo
- {
- public int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException //声明可能会出现问题
- {
- int [] arr=new int[a];
- System.out.println(arr[4]);
- return a/b;
- }
- }
- class ExceptionDemo
- {
- public static void main(String[] args)
- {
- Demo d = new Demo();
- try
- {
- int x=d.div(4,0);
- System.out.println("x="+x);
- }
-
- catch (Exception e)
- {
- System.out.println("haha:"+e.toString());
- }
- catch(ArithmeticException e)
- {
- System.out.println(e.toString());
- System.out.println("快零除了");
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- System.out.println(e.toString());
- System.out.println("角标越界了");
- }
-
- System.out.println("over");
- }
- }
复制代码 我看视频中有点没弄明白,把父类异常catch (Exception e)放在最上面的话,为什么编译都通不过?我认为应该是父类异常执行后,下面的2个子类catch就不执行了。为什么通不过呢,是不是我理解错了 |