package com.oned9z;
/**
* @program: com.oned9z
* @description:使用throw抛出年龄异常
* @author: Mr.Lin
* @create: 2019年7月23日
**/
public class Person {
private int age;

public int getAge() {
return age;
}

public void setAge(int age) throws Exception{
if(age>=1 && age<=100){
this.age = age;
}else{
throw new Exception("年龄必须在1—100之间!");
}

}

}

package com.oned9z;
/**
* @program: com.oned9z
* @description:测试类
* @author: Mr.Lin
* @create: 2019年7月23日
**/
public class TestAge {
public static void main(String[] args) {
Person person = new Person();
try{
person.setAge(1231312);
}catch (Exception e){
e.printStackTrace();
}
}

}

 

posted on 2019-07-23 18:16  MOCHICBEAM  阅读(234)  评论(0)    收藏  举报