异常

class User{
    private int age;

    public void setAge(int age){
        if(age<0){
            Exception r=new Exception("年龄不能为负数");
            throw e;
        }
        this.age=age;
    }
}
class Test{
        public static void main(String arg []){
            User user =new User();
            try{
                user.setAge(-20);
            }catch(Exception e){
                System.out.println(e);
            }
        }
}

 

posted on 2015-11-25 18:12  凡一二三  阅读(130)  评论(0)    收藏  举报