package J20250718.demo02_exception; /** * @author WanJl * @version 1.0 * @title Person * @description * @create 2025/7/18 */ public class Person { private String name; private Integer age; private String sex; //性别 private double height; //身高 public String getName() { return name; } public void setName(String name) throws NameSizeException{ if (name==null){ throw new NullPointerException();//空指针异常 } if (name.length()<=0||name.length()>15){ throw new NameSizeException(); } this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) throws AgeException{ //throws的作用:声明可能引发的异常类 if(age<=0||age>145){ //throw的作用:抛出创建好的异常对象。 throw new AgeException("年龄超过限制了"); //构建出一个异常对象, 如果满足条件了就构建异常对象。 } this.age = age; } }