自定义异常分为两种异常:编译时异常 以及 运行时异常
关于下定义异常:分为以下几个步骤:
1:定义一个异常类 extends Exception 或 RunTimeException
2:重写构造器
3:再出现异常的地方使用throws new 自定义异常
4:抛出异常
public class CodingTest {
public static void main(String[] args) {
}
public static void checkAge(int age) throws Exception{
if(age>0||age>120){
throw new MyThrow("年龄异常");
}
}
}
class MyThrow extends Exception{
public MyThrow() {
}
public MyThrow(String message) {
super(message);
}
}