public static void main(String[] args) {
Human man = new Human();
try {
man.setAge(120);
} catch (Exception e) {
e.printStackTrace();
}
}
class Human{
private int age ;
public void setAge(int age) throws Exception {
if(age < 0 || age > 110 ){
throw new Exception("非法年齡");
}
this.age = age;
}
@Override
public String toString() {
return "我的年齡是: "+age;
}
}
使用throw new Exception 向外層拋異常
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.