java自定义一个年龄的异常

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();
            }


    }
}

}

猜你喜欢

转载自blog.csdn.net/weixin_42337796/article/details/81901141