public static void main(String[] args) {
try {
saveAge(160);
System.out.println("底层执行成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("底层出现了bug");
}
}
public static void saveAge(int age) {
if (age > 0 && age < 150) {
System.out.println();
} else {
//用一个异常对象封装这个问题
//throw 抛出去这个异常对象
throw new AgeIllegalRuntimeException("年龄非法,你的年龄是:" + age);
}
}
}
自定义异常类
//必须让这个类继承RuntimeException
public class AgeIllegalRuntimeException extends RuntimeException{
public AgeIllegalRuntimeException() {
}
public AgeIllegalRuntimeException(String message) {
super(message);
}
}