class MyException extends Exception
{
private int num;
MyException(){}
MyException(String message)
{
super(message);
}
MyException(String message,int num)
{
super(message);
this.num = num;
}
public int getNum()
{
return num;
}
}
class MathDemo
{
public int divide(int a,int b) throws MyException
{
if(b<0)
{
throw new MyException("除数不能为负数",b);
}
else
{
return a/b;
}
}
}
class ExceptionDemo8
{
public static void main(String[] args) //throws MyException
{
MathDemo md = new MathDemo();
try
{
System.out.println(md.divide(10,-2));
}
catch(MyException me)
{
System.out.println("你提供的除数是"+me.getNum()+",而我们要求"+me.getMessage());
}
}
}
红色标记的为什么那么用为什么不调过来用呢 throw 和 throws |