//自定义异常:throws与throw
//为什么输出的异常信息没有出现错误的数字。
class fushu extends Exception
{
private String message;
private int num;
fushu()
{
}
fushu(String message,int num)
{
this.message=message;
this.num=num;
}
public String getMessage()
{
return message;
}
public int getNum()
{
return num;
}
}
class yy
{
int x;
int y;
yy(int x,int y)
{
this.x=x;
this.y=y;
}
void mai() throws fushu
{
if(y<0)
{
throw new fushu("出负数了呀!",y);
}
int u=x/y;
System.out.print(u);
}
}
public class stu {
public static void main(String[] args) {
yy g=new yy(4,-1);
try
{
g.mai();
}
catch(fushu e)
{
System.out.println(e.toString());
System.out.println(e.getNum());
}
System.out.print("over!");
}
}
|
|