0130 异常机制

0130 异常机制_第1张图片

0130 异常机制_第2张图片 

0130 异常机制_第3张图片 

0130 异常机制_第4张图片 

0130 异常机制_第5张图片 

package Demo0130;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) throws AgeException {
        setName(name);
        setAge(age);
    }

    public Person() {
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if (age > 0 && age < 150){
            this.age = age;
        }else{
            throw new AgeException("年龄不合理!");
        }
    }
}
package Demo0130;

public class PersonTest {
    public static void main(String[] args) throws AgeException {
        Person person = null;
        try {
            person = new Person("张辉",10);
        } catch (AgeException e) {
            e.printStackTrace();
        }
        System.out.println(person);
    }
}

 

你可能感兴趣的:(java,前端,开发语言)