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(3,-1);
System.out.println(x);
}
catch(FuShuException e)
{
System.out.println(e.toString());
System.out.println("输入的除数为负数");
}
System.out.println("over");
}
}
我去从装JDK试试,
|