class ExceptionTest3//自定义异常练习
{
public static void main(String[] args)
{
Test t=new Test();
try
{
int x=t.div(4,-1);
System.out.println("x="+x);
}
catch(FushuException e)
{
System.out.println(e.toString());
System.out.println("除数出现负数");
}
System.out.println("over");
}
}
class FushuException extends Exception
{
private String msg;
FushuException(String msg)
{
this.msg=msg;
}
public String getMessage()
{
return msg;
}
}
class Test
{
public int div(int a,int b)throws FushuException
{
if(b<0)
throw new FushuException("除数是负数的情况/by fushu");
return a/b;
}
}
在最开始加入
java.io.IOException