黑马程序员技术交流社区

标题: 我犯的小错误 呵呵 [打印本页]

作者: 谢明    时间: 2012-2-20 00:18
标题: 我犯的小错误 呵呵
  1. class FuShuException extends Exception
  2. {
  3.         private int value;

  4.         FuShuException()
  5.         {
  6.                 super();
  7.         }
  8.         FuShuException(String msg,int value)
  9.         {
  10.                 super (msg);
  11.                 this.value=value;
  12.         }
  13.         public int getValue()
  14.         {
  15.                 return value;
  16.         }
  17. }
  18. class Demo
  19. {
  20.         int div(int a,int b)throws FuShuException
  21.         {
  22.                 if(b<0)
  23.                         throw new FuShuException("出现了负数",b);
  24.                 return a/b;
  25.         }

  26. }
  27. class ExceptionDemo3
  28. {
  29.         public static void main(String[] args)throws FuShuException
  30.         {
  31.                 Demo d=new Demo();
  32.                
  33.                 try
  34.                 {
  35.                         int x=d.div(4,-1);
  36.                         System.out.println("x="+x);
  37.                 }
  38.                 catch (Exception e)
  39.                 {
  40.                        
  41.                         System.out.println(e.toString()+e.getValue());
  42.                         //System.out.println(e.toString());
  43.                         //e.printStackTrace();

  44.                 }

  45.                 System.out.println("over");
  46.         }
  47. }
复制代码
无法编译通过的  大家猜猜是哪里  :lol
作者: 陈建凡    时间: 2012-2-20 00:56
System.out.println(e.toString()+e.getValue());
作者: 谢明    时间: 2012-2-20 01:46
不对哦   是异常方面的哦  {:soso_e144:}
作者: 花开~的季节    时间: 2012-2-20 07:29
Exception换成FuShuException,此外上面的主函数抛出的异常去掉,因为处理了
作者: 谢明    时间: 2012-2-20 08:22
嗯  确实 现在求解  FuShuException继承了Exception,为什么 不能通过呢
作者: 王康    时间: 2012-2-20 10:46
1,你自定义了FuShuException 异常函数,继承了Exception函数.
而getValue()方法在父类Exception中并没有次方法,是你在子类FuShuException中新定义的方法.
那么
class ExceptionDemo3
{
        public static void main(String[] args)throws FuShuException
        {
                Demo d=new Demo();
               
                try
                {
                        int x=d.div(4,-1);
                        System.out.println("x="+x);
                }
                catch (Exception e) //此处应该是把Exception 换成 FuShuException
                {
                        
                        System.out.println(e.toString()+e.getValue());
                        //System.out.println(e.toString());
                        //e.printStackTrace();

                }

                System.out.println("over");
        }
}

作者: 刘基军    时间: 2012-2-20 11:02
不构成多态,所以编译不通过。
作者: 谢明    时间: 2012-3-15 14:43
{:soso_e113:}




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