黑马程序员技术交流社区
标题:
我犯的小错误 呵呵
[打印本页]
作者:
谢明
时间:
2012-2-20 00:18
标题:
我犯的小错误 呵呵
class FuShuException extends Exception
{
private int value;
FuShuException()
{
super();
}
FuShuException(String msg,int value)
{
super (msg);
this.value=value;
}
public int getValue()
{
return value;
}
}
class Demo
{
int div(int a,int b)throws FuShuException
{
if(b<0)
throw new FuShuException("出现了负数",b);
return a/b;
}
}
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)
{
System.out.println(e.toString()+e.getValue());
//System.out.println(e.toString());
//e.printStackTrace();
}
System.out.println("over");
}
}
复制代码
无法编译通过的 大家猜猜是哪里 :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