`

Exception 自定义

阅读更多

public class AgeOutofBoundsException extends Exception{
private static final long serialVersionUID = 8299254917638606334L;

 

public AgeOutofBoundsException(){
super("年龄超出范围");
}

public AgeOutofBoundsException(String message) {
super(message);
}
}

 

 

public class People {
private int age;


public People( int age,) throws AgeOutofBoundsException {
if(age<1 || age>200)
{
throw new AgeOutofBoundsException();
}
this.age = age;
}

 

 

public void setAge(int age) throws AgeOutofBoundsException {
if(age<1 || age>200)
{
throw new AgeOutofBoundsException("年龄不在1--200之间:"+age);
}
this.age = age;
}


public int getAge() {
return age;
}


}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics