Use throw an exception thrown Age

com.oned9z Package Penalty for;
/ **
* @program: com.oned9z
* @description: Use throw an exception thrown Age
* @author: Mr.Lin
* @Create: 2019 Nian 7 Yue 23 Ri
** /
public class {the 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();
}
}

}

 

Guess you like

Origin www.cnblogs.com/lpbk/p/11233575.html