在相乘等于100时,调用throw MulException类抛出异常。
在相除等于2时,调用throw DivException类抛出异常。
class MulException extends Exception{
private int a;
MyException( int a ){
a= a;
}
public String toString(){
return "MulException:"+a;
}
}
public class DefineExceptionDemo{
static void compute(int a,int b) throws MulException {
System.out.println("调用compute");
if( a*b == 100 )
throw new MulException (a*b);
System.out.println("这是正常退出");
}
public static void main( String args[] ){
try{
compute(10,10);
compute(20,20);
}catch(MyException e){
System.out.println("此处捕获了"+e);
}
}
}