作业2

 

 

package Exception;

public class Demo04 {
    
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException{
        if(age>=1&&age<=100) {
        this.age = age;
        }else {
            
            throw new AgeException("年龄必须在1到100之间!");
            
            
        }
    }
    
    public static void main(String[] args) {
        
        Demo04 d = new Demo04();
        try {
            d.setAge(194);
        } catch (AgeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

 

package Exception;

public class AgeException extends Exception{

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

 

 

 

posted @ 2019-07-22 17:13  AxeBurner  阅读(80)  评论(0编辑  收藏  举报