异常的抛出 Exception

 我可以处理异常的时候

用   try...catch 句尝试修复

也可以 继承extends Exception

Exception 是所异常类的父类

public class Tast1 {
    public static void main(String[] args) {
        //目标掌握自定义异常以及异常的作用

        try {
            savAge(166);
            System.out.println("底层执行成功!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("底层出现bug!");
        }


    }
    //1.保存一个合法的年龄
    public static void savAge(int age){
        if (age>0&&age<150){
            System.out.println("年龄录入成功!:"+age);
        }else {
            //用一个异常对象封装这个问题

          throw   new AgeIllegalruntimeException("age is illegal your is:"+age);
        }
    }
}

 我们也可以定义封装异常对象来使用

public class AgeIllegalruntimeException  extends RuntimeException{
    public AgeIllegalruntimeException() {
    }

    public AgeIllegalruntimeException(String message) {
        super(message);
    }
}

你可能感兴趣的:(java,前端,服务器)