class Student{
private int age;
public void setAge(int age) throws Exception{
if(age>0&&age<150){
this.age = age;
}else{
//1.System.out.println("年龄不正确");
//出现异常,终止程序运行
throw new RuntimeException("年龄不正确");
}
}
}
public class Test1 {
public static void main(String[] args) throws Exception {
Student stu = new Student();
//1.必须处理异常
/*try{
stu.setAge(-10);
}catch(Exception e){
e.printStackTrace();
}*/
//1.继续往上抛
stu.setAge(-10);
}
}
运行时异常,编译时异常
最新推荐文章于 2024-07-07 10:09:08 发布