黑马程序员技术交流社区

标题: 关于异常处理问题,求解...? [打印本页]

作者: xiaobo    时间: 2014-2-24 10:53
标题: 关于异常处理问题,求解...?
这段程序哪里出了错,为啥编译失败?
  1. class Demo
  2. {
  3.         int div(int a,int b)   
  4.         {
  5.            int[]arr = new int[a];
  6.            System.out.println(arr[4]);
  7.            return a/b;
  8.         }
  9. }
  10. class          ExceptionDemo2
  11. {
  12.         public static void main(String[] args)
  13.         {
  14.                  Demo d = new Demo();
  15.                  try
  16.                  {
  17.                         int x = d.div(5,0);
  18.                          System.out.println("x="+x);
  19.                  }
  20.                  catch (Exception e)
  21.                  {
  22.                         System.out.println("haha:"+e.toString());
  23.                  }
  24.                  catch(ArithmeticException e )
  25.                 {
  26.                   System.out.println(e.toString());
  27.                   System.out.println("被零除啦");
  28.                 }
  29.                 catch(ArrayIndexOutOfBoundsException e)
  30.                 {
  31.                   System.out.println(e.toString());
  32.                   System.out.println("角标越界啦!!!");
  33.                 }
  34.                   System.out.println("Over");
  35.         }
  36. }
复制代码




作者: flying    时间: 2014-2-24 13:16
catch  是从上往下执行的   catch(Exception e)了下面就不用再次catch了  如果要捕获异常 应先从异常的子类开始写  放后面就可以了
  1. class Demo
  2. {
  3.         int div(int a,int b)   
  4.         {
  5.            int[]arr = new int[a];
  6.            System.out.println(arr[4]);
  7.            return a/b;
  8.         }
  9. }
  10. public class   ExceptionDemo2
  11. {
  12.         public static void main(String[] args)
  13.         {
  14.                  Demo d = new Demo();
  15.                  try
  16.                  {
  17.                         int x = d.div(5,0);
  18.                          System.out.println("x="+x);
  19.                  }

  20.                  catch(ArithmeticException e )
  21.                 {
  22.                   System.out.println(e.toString());
  23.                   System.out.println("被零除啦");
  24.                 }
  25.                 catch(ArrayIndexOutOfBoundsException e)
  26.                 {
  27.                   System.out.println(e.toString());
  28.                   System.out.println("角标越界啦!!!");
  29.                 }
  30.                  catch (Exception e)
  31.                  {
  32.                         System.out.println("haha:"+e.toString());
  33.                  }
  34.                  System.out.println("Over");
  35.         }
  36. }
复制代码

作者: qqwwdr    时间: 2014-2-24 13:25
zengming13 发表于 2014-2-24 11:01
因为处理异常时应该先处理子类异常
Exception是ArithmeticException 的父类,当然不行啦[/back ...

Exception包含了基本上所有的可能的异常,
ArithmeticException 放在后面,代码是不可能到达这里处理这个异常。
可以这样理解么?

子类异常放到前面单独来处理,用于一些比较特别要注意的情况
作者: 一年_Hei    时间: 2014-2-24 13:50
catch (Exception e) 放在最后一个就好了,Exception它能捕捉到一切异常






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