自定义异常

自定义异常:
package com.softeem.len11;
//用户自定义异常《年龄异常》
public class AgeEcxeption extends Exception{
    public AgeEcxeption(){//无参构造器
        super();//调用父类的无参构造器
    }
    public AgeEcxeption(String msg){//有参构造器
        super(msg);//调用父类的有参构造器
    }
}
package com.softeem.len11;
import java.io.IOException;
public class Students extends Person{
    public int age;
    public void getName()throws IOException{//异常重写
        //重写的异常不能比被重写的异常更大
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age) throws AgeEcxeption{
        if (age>17&&age<30) {
            this.age=age;
        }else {
            throw new AgeEcxeption("学生年龄不合理");
        }
    }
    public static void main(String[] args) {
        Students s=new Students();
        try {
            s.setAge(900);
            System.out.println("学生的年龄:"+s.getAge()+"岁");
        } catch (AgeEcxeption e) {
            e.printStackTrace();
        }
        
        
    }
}

 

posted @ 2014-12-13 00:09  屌丝一枚#  阅读(110)  评论(0)    收藏  举报