public class TestException {
public static void main(String[] args){
int a=0,b=0;
TestException te=new TestException();
try{
te.add(a,b);
}
catch(MyException e){
e.printStackTrace();
}
}
public int add(int a,int b)throws MyException{
if(a==0 && b==0){
throw new MyException("两个数不能同时为零=口=!");
}
return a+b;
}
class MyException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
public MyException(String msg){
super(msg);
}
}
}
创建自定义异常类
最新推荐文章于 2022-12-26 09:01:48 发布