运行时异常,编译时异常

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);

    }

}

猜你喜欢

转载自blog.csdn.net/xiaoblank/article/details/81950981