javaSE之运行时异常和编译时异常

运行时异常继承自RuntimeException;

package foundationEnhance;


public class Person {
    private int age;

    public Person(int age) {
        super();
        if(age<0){
            throw new ageException("年龄不可以为负数!");
        }
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age<0){
            throw new ageException("年龄不可以为负数!");
        }
        this.age = age;
    }
}

 

package foundationEnhance;

public class testPerson {
    public static void main(String[] args) {
        Person p=new Person(-1);
        p.setAge(-2);
        System.out.println(p.getAge());
    }
}

 

package foundationEnhance;

public class ageException extends RuntimeException{
    public ageException() {

    }

    public ageException(String message) {
        super(message);
    }
}

 

编译时异常继承自Exception;

 看看前面的视频,关于运行时异常和编译时异常;

...未完待补充

posted @ 2019-06-30 23:54  贰零一八  阅读(137)  评论(0编辑  收藏  举报