A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 海世山盟 中级黑马   /  2014-3-26 20:53  /  926 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文



        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()我已经抛出了异常,后面调用的时候我并没有捕获和处理,按照老师的讲解是,后面的调用时我并没有捕获和处理异常就会报错的啊。可是我异想天开的运行了一下。结果发现并没有出现任何错误。这是什么情况?谁解答下

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

4 个回复

倒序浏览
public double div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{只是告诉程序这个方法可能会抛出这些个异常
回复 使用道具 举报
年轻的老头 发表于 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:21
public double div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException{只是告诉程序 ...

原来那两个是运行期异常,非常的特殊,可以不用处理。
回复 使用道具 举报
  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. //如果我这么说 你还不明白那么可以好好看书 或把疑惑再次指出 我好解答

复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马