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 memo
{
int div(int a,int b)throws FuShuException
{
if(b<0)
throw new FuShuException("出现了除数是负数的情况------ / by fushu",b);
//手动通过throw关键字抛出一个自定义异常对象。
return a/b;
}
}
class jh
{
public static void main(String[] args)
{
memo d = new memo();
try
{
int x = d.div(4,2);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
System.out.println("错误的负数是:"+e.getValue());
}
System.out.println("over");
}
}
这个throw抛出和常规的return返回有什么区别吗?或者说这个throw可以用return返回调用吗? |