public class AgeException extends Exception{
public AgeException(String msg){
super(msg);
}
}
public class AgeExceptionTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“请输入你的年龄:”);
int age = input.nextInt();
if(age>=0 && age<=100){
System.out.println(“年龄合法”);
}else{
try {
throw new AgeException("年龄必须在0-100之间");
} catch (AgeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}