class FuShuException extends Exception//自定义一个异常并继承异常类
{
private String msg;
FuShuException(String msg)
{
this.msg=msg;
}
public String getMessage()
{
return msg;
}
}
class Demo
{
int div(int a,int b)throws FuShuException
{
if(b<0)
throw new FuShuException("出现了除数为负数情况");//手动通过throw关键字抛出一个自定义异常
return a/b;
}
}
class ExceptionDemo
{
public static void main(String[] args)
{
Demo d=new Demo();
try
{
int x=d.div(4,-1);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
System.out.println("除数出现负数了");
}
}
}
问一下程序中throws FuShuException这句话是什么作用??有点不明白
|
|