class FuShuException extends Exception //getMessage();
{
private int value;
FuShuException()
{
super();
}
FuShuException(String msg,int value)
{
super(msg);
this.value = value;
}
public int getValue()
{
return value;
}
}
class Demo3
{
int div(int a,int b)throws FuShuException
{
if(b<0)
throw new FuShuException("出现了除数是负数的情况------ / by fushu",b);
//手动通过throw关键字抛出一个自定义异常对象。
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args)
{
Demo3 d = new Demo3();
try
{
int x = d.div(4,-9);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
//System.out.println("除数出现负数了");
System.out.println("错误的负数是:"+e.getValue());
}
System.out.println("over");
}
}
复制代码
此段代码,Eclipse环境下,在毕老师的原包中运行会出现
Exception in thread "main" java.lang.NoSuchMethodError:
codes.day09.FuShuException.<init>(Ljava/lang/String;I)V
at codes.day09.Demo3.div(ExceptionDemo3.java:82)
at codes.day09.ExceptionDemo3.main(ExceptionDemo3.java:97)
把代码考呗到其他包运行正常,结果为:
com.itheima.FuShuException: 出现了除数是负数的情况------ / by fushu
错误的负数是:-9
over