黑马程序员技术交流社区

标题: 异常??求解??不同一个?? [打印本页]

作者: 高昌德    时间: 2014-12-5 20:39
标题: 异常??求解??不同一个??
  1. class Demo
  2. {
  3.         public int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException,Exception
  4.         {
  5.                 int[] arr = new int[a];
  6.                 System.out.println(arr[5]);
  7.                 return a/b;
  8.         }
  9. }
  10. public class ExceptionDemo {

  11.         public static void main(String[] args)
  12.         {
  13.                 Demo d = new Demo();
  14.                 try
  15.                 {
  16.                         int x = d.div(5,1);
  17.                         System.out.println(x);
  18.                 }
  19.                 catch(ArithmeticException e)
  20.                 {
  21.                         System.out.println("除0了");
  22.                         System.out.println(e.toString());
  23.                 }
  24.                 catch(ArrayIndexOutOfBoundsException e1)
  25.                 {
  26.                         System.out.println("角标越界了");
  27.                         System.out.println(e1.toString());
  28.                 }
  29.                 System.out.println("over");
  30.         }
  31. }
复制代码
为什么已经对int x = d.div(5,1);进行处理了,编译还是提示要进行异常处理?

作者: kerner    时间: 2014-12-5 21:21
int x = d.div(5,1);抛出三个异常,而你自己只处理了2个,Exception没有处理。
作者: 禾青青    时间: 2014-12-9 23:38
每天都来看一看




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2