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


}

 

你可能感兴趣的:(java,exception)