classFuShuExceptionextendsException//getMessage();{privateStringmsg;FushuException(Stringmsg){this.msg=msg;}publicStringgetMessage(){returnmsg;}}classDemo{intdiv(inta,int...
class FuShuException extends Exception//get Message();
{
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("除数出负数了");
return a/b;
}
}
class ExceptionDemo3
{
public static void main(String[] args) //throws Exception
{
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("除数出现负数了");
}
System.out.println("over");
}
}
展开