public class TestException {
public static void main(String[] args) {
int[] arr = {11,22,33};
try {
System.out.println(arr[5]);
} catch (Exception ex) {}
try {
//saveAge(100);
saveAge(200);
System.out.println("底层是成功执行的");
} catch (Exception e) {
e.printStackTrace();
System.out.println("底层出现了bug");
}
try {
saveAge2(200);
System.out.println("saveAge2底层是成功执行的");
} catch (AgellegalException e) {
e.printStackTrace();
System.out.println("saveAge2底层出现了bug");
}
}
public static void saveAge(int age) {
if (age > 0 && age < 150) {
System.out.println("年龄被成功保存" + age);
} else {
throw new AgellegalRuntimeException("/age is illegal ,you age is" + age);
}
}
public static void saveAge2(int age) throws AgellegalException {
if (age > 0 && age < 150) {
System.out.println("年龄被成功保存" + age);
} else {
throw new AgellegalException("/age is illegal ,you age is" + age);
}
}
}
class AgellegalException extends Exception {
public AgellegalException() {
}
public AgellegalException(String message) {
super(message);
}
}
class AgellegalRuntimeException extends RuntimeException {
public AgellegalRuntimeException() {
}
public AgellegalRuntimeException(String message) {
super(message);
}
}
如何应用,
实际工作当中, 强烈需要程序员处理该异常的就抛一个编译型异常;
一般情况就用运行时异常。
标签:AgellegalException,java,处理,age,System,println,异常,public,out From: https://blog.csdn.net/m0_64134481/article/details/137603256