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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 谢明 中级黑马   /  2012-2-20 00:18  /  2434 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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

7 个回复

倒序浏览
System.out.println(e.toString()+e.getValue());
回复 使用道具 举报
不对哦   是异常方面的哦  {:soso_e144:}
回复 使用道具 举报
Exception换成FuShuException,此外上面的主函数抛出的异常去掉,因为处理了
回复 使用道具 举报
嗯  确实 现在求解  FuShuException继承了Exception,为什么 不能通过呢
回复 使用道具 举报
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:21
7#
不构成多态,所以编译不通过。
回复 使用道具 举报
谢明 中级黑马 2012-3-15 14:43:46
8#
{:soso_e113:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马