JavaSE the time of abnormal operation and compile-time anomaly

Abnormal inherited from RuntimeException runtime;

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 new ageException ( "Age can not be negative!" ); 
        } 
        the 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);
    }
}

 

 

Abnormal inherited from Exception compile time;

 Look at the front of the video, on a runtime exception when compiling and abnormal;

To be added ... unfinished

Guess you like

Origin www.cnblogs.com/shijinglu2018/p/11111847.html