黑马程序员技术交流社区

标题: 关于异常的处理 [打印本页]

作者: 海世山盟    时间: 2014-3-26 20:53
标题: 关于异常的处理


        class Test{
                int x=5;
                public double div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{
                       
                        return a/b;
                }
   class inner{
                        int x=8;
                        void method(){
                                System.out.println(x);
                                }
                        }
                /*
                 * void function(){
                 
                       
                        new inner().method();
                }
        */
                }
                class Demo{
                        public static void main(String[] args){
                                Test t=new Test();
                                //t.function();
                                new Test().new inner().method();
                                double s=t.div(5,1);
                                System.out.println(s);
                                }
                               
                               
前面的div()我已经抛出了异常,后面调用的时候我并没有捕获和处理,按照老师的讲解是,后面的调用时我并没有捕获和处理异常就会报错的啊。可是我异想天开的运行了一下。结果发现并没有出现任何错误。这是什么情况?谁解答下

作者: 年轻的老头    时间: 2014-3-26 21:21
public double div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{只是告诉程序这个方法可能会抛出这些个异常
作者: 海世山盟    时间: 2014-3-26 21:37
年轻的老头 发表于 2014-3-26 21:21
public double div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{只是告诉程序 ...

                class Demo{
                                        public static void main(String[] args){
                                                double s=div(5,1);
                                                System.out.println(s);
                                               
                                               
                        }
                       
                        public static double div(int a,int b)throws Exception
                       
                        return a/b;
                }
                                }
但是为什么这个就会提示必须捕获处理异常呢?异常的捕获和处理究竟是怎么弄得?
作者: 海世山盟    时间: 2014-3-26 21:48
年轻的老头 发表于 2014-3-26 21:21
public double div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{只是告诉程序 ...

原来那两个是运行期异常,非常的特殊,可以不用处理。
作者: caijunsong    时间: 2014-3-27 01:00
  1. class Test
  2. {
  3.         int x=5;
  4.         public double div(int a,int b)
  5.         throws ArithmeticException,ArrayIndexOutOfBoundsException
  6.         {
  7.                 //if(b==0)
  8.                 //throw RuntimeException("除数为0");
  9.                 return a/b;
  10.         }
  11.         class inner
  12.         {   
  13.                 int x=8;
  14.                 void method()
  15.                 {
  16.                         System.out.println(x);
  17.                 }
  18.         }
  19. }
  20. public class Demo
  21. {
  22.         public static void main(String[]agrs)
  23.         {
  24.                 Test t=new Test();
  25.                 new Test().new inner().method();
  26.                 double s=t.div(5,1);//试着把1改成0  试试?
  27.                 System.out.println(s);
  28.         }
  29. }
  30. //首先楼主,你就没有正真理解throw和throws两者的真正用法,你在div方法后面用throws声明的抛出了两个异常
  31. //类,这个只是作为声明并不是创建异常对象,我们用try。。catch。。来捕获异常对象并处理这个异常对象
  32. //在你的Demo中你给dic传入的值分别是5和1,5/1它并没有违反算术规则,因此不会产生算术异常对象,既然没有
  33. //产生对象所以你在Demo中有没有把 double s=t.div(5,1);放在try。。catch中都无所谓啊!!

  34. //如果你把1改成0它就会抛出算术异常,那么你的代码就会报错了,当然我们也可以通过throw来抛出自定义异常对象
  35. //如果我这么说 你还不明白那么可以好好看书 或把疑惑再次指出 我好解答

复制代码





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