Exception 自定义

来源:互联网 发布:linux基础试题及答案 编辑:程序博客网 时间:2024/05/09 16:27

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

  
}