首页 > 其他 > 详细

throws 和 throw

时间:2016-07-31 01:38:07      阅读:155      评论:0      收藏:0      [点我收藏+]
package unit5;

public class Person {
    private int age;
    private String name;
    public String getName() {
        return name;
    }
    public int  getAge() {
        return age;
    }
    public void setAge(int a) throws Exception {
        if(a<0){
            throw new Exception("出现异常,年龄不能小于零");
        }
        if(a>1000){
            throw new Exception("出现异常,年龄不能大于1000");
        }
        age = a;
    }
    public void setName(String name) {
        this.name=name;
    }
    public void display() {
        System.out.println("我叫 "+name+"今年 "+age+" 岁。");
    }

}

 

package unit5;

public class Throwsdeml {
    public static void main(String[] args) {

        Person zhang = new Person();
        try{
            zhang.setName("张三");
            zhang.setAge(2000);
            zhang.display();
        }catch (Exception e) {

            System.out.println(e.getMessage());
        }
    
    }
}

 

throws 和 throw

原文:http://www.cnblogs.com/superxuezhazha/p/5722126.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!