class Age {
public void setAge(int age) throws Exception{
if (age < 12 && age > 0){
System.out.println("This is a child");
}else if(age >= 18){
System.out.println("This is a adult");
}else if(age >=12 && age < 18){
System.out.println("This is a youth");
}else{
Exception e = new Exception("The age can't be negative number");
throw e;
}
}
}
public class TestDemo {
public static void main(String args[]){
Age a = new Age();
try{
a.setAge(-1);
}
catch(Exception e){
System.out.println(e);
}
finally {
System.out.println("The End");
}
}
}
try catch finally
最新推荐文章于 2023-10-19 10:53:19 发布