class MyException extends Exception//自定义异常,继承自:Exception
{
private int num;
public MyException(String msg,int num)
{
super(msg);
this.num=num;
}
public int getNum()
{
return this.num;
}
}
class Test
{
public static void reg(int num) throws MyException//重点是词句。throws MyException 不可缺少。
{
if(num<0)
{
throw new MyException("num不可小于0",num);//非法条件,抛出自定义的异常。
}
print("reg num::"+num);
}
public static void manage()
{
try
{
reg(100);
}
catch (MyException me)//捕获自定义的异常。
{
print("feifa num:"+me.getNum());
}
}
public static void main(String[] args)
{
Test t=new Test();
t.manage();
}
}