java自定义异常要继承Exception类:具体实现如下
public class ExceptionTest {
public static void main(String[] args) throws MyException {
ExceptionTest t = new ExceptionTest();
t.test(-1);
}
public void test(int a) throws MyException{
if(a < 0 ){
throw new MyException("为负值,不合理",1);
}
}
}
class MyException extends Exception{
private int id;
public MyException(String message,int id){
super(message);
id = this.id;
}
public int getId(){
return id;
}
}