package com.it;
/**
* 测试自定义异常
*/
public class Test03 {
public static void main(String[] args) throws IllegalAgeException {
Person p = new Person();
p.setAge(99);
System.out.println(p.getAge());
}
}
/**
* 不合法年龄异常
*/
class IllegalAgeException extends Exception{
// 默认无参构造器
public IllegalAgeException(){};
public IllegalAgeException(String message){
super(message);
}
}
class Person {
private String name;
private int age;
public void setAge(int age) throws IllegalAgeException {
if (age<0){
throw new IllegalAgeException("人的年龄不能小于0");
}
this.age = age;
}
public int getAge(){
return this.age;
}
}