代码如下:
public class NetBar {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if (age < 18) {
throw new Exception("年龄未满18,不能进入网吧。");
} else {
throw new Exception("年龄合格");
}
}
public static void main(String[] args) {
NetBar netBar = new NetBar();
try {
netBar.setAge(16);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
注:主页更多学习资料。