使用throw抛出年龄异常

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();
}
}

}

猜你喜欢

转载自www.cnblogs.com/lpbk/p/11233575.html