//如何自定义一个异常类
/*1.自定义的异常类继承现有的异常类
- 2.提供一个序列号,提供构造器
*/
public class MyException extends Exception{
static final long serialVersionUID = -7034897190745766939L;
public MyException() {
super();
}
public MyException(String msg) {
super(msg);
}
}
public class TestStudent {
public static void main(String[] args) {
Student t =new Student();
try {
t.regist(-2);
}catch(MyException e) {
System.out.println(e.getMessage());
}
}
}
class Student {
int id;
public void regist(int id) throws MyException{
if(id > 0) {
this.id = id;
}else {
throw new MyException("传入的学号有误");
}
}
}