妖碧落

导航

自定义异常

自定义异常就为了一个名字而已

public void setAge(int age) throws AgeOutOfBoundsException {
        if (age > 0 && age <120) {
            this.age = age;
        } else {
            throw new AgeOutOfBoundsException("年龄非法");
        }
        
    }
    
}
class AgeOutOfBoundsException extends Exception{
    public AgeOutOfBoundsException(String dec) {
        super(dec);
        
    }
    
}

 

posted on 2019-08-06 00:05  妖碧落  阅读(92)  评论(0编辑  收藏  举报