绊夏微凉

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Java异常

​ 异常的超类为Throwable,类似于类中的Object,异常分为两大类:Error和Exception,Error会导致程序终止,问题很严重,但无法处理,一般处理Exception。

异常关键字

try catch finally:用于捕获异常

举例1:try catch绑定在一起,捕获异常时必须存在,finally可有可无,无论有没有发生异常,finally中的代码块一定会执行,finally中的代码块的作用是善后,如IO流的关闭。

public void num() {
        // try catch
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            System.out.println("try catch");
        }
        
        // try catch finally
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            System.out.print("try catch ");
        } finally {
            System.out.println("finally");
        }

        // finally
        try {
            int i = 10 / 1;
        } catch (Exception e) {
            System.out.println("try catch");
        } finally {
            System.out.println("finally");
        }
    }

举例2:可以存在多个catch,但捕获异常的顺序从小到大,并且只会被一个异常捕获

public  void num() {
        // ArithmeticException
        try {
            int i = 10 / 0;
        } catch(ArithmeticException e) {
            System.out.println("ArithmeticException");
        } catch (Exception e) {
            System.out.println("Exception");
        } catch (Throwable e) {
            System.out.println("Throwable");
        }
    }

throw:用于在方法块中抛出异常

public class Application {
    public static void main(String[] args) {
        divide(1, 0);
    }
    public static int divide(int num1, int num2) {
        if (num2 == 0) {
            throw new ArithmeticException();
        }
        return num1 / num2;
    }
}

throws:用于方法抛出异常

public class Application {
    public static void main(String[] args) {
        System.out.println(divide(1, 0 ));
    }
    public static int divide(int num1, int num2) throws ArithmeticException{
        return num1 / num2;
    }
}

自定义异常

​ 自定义异常需要继承异常

举例1:自定义异常文件需重写toString方法,e.printStackTrace():会自动调用自定义类的toString方法

// MyException.java文件
public class MyException extends Exception {
    private String msg;
    MyException() {
    }
    MyException(String msg) {
        this.msg = msg;
    }
    @Override
    public String toString() {
        return "MyException{" +
                "msg='" + msg + '\'' +
                '}';
    }
}
// Application.java文件
public class Application {
    public static void main(String[] args) {
        //MyException{msg='151'}
        getAge(151);
    }
    public static void exceptionHandle(int age) throws MyException {
        if (age < 0 || age > 150) {
            throw new MyException(age + "");
        }
    }
    public static void getAge(int age) {
        try {
            exceptionHandle(age);
            System.out.println(age);
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

}

举例2:继承异常为RuntimeException及其子类,不会被强制要求处理,继承除RuntimeException及其子类,会被强制要求处理,要么抛出异常,要么捕获异常,不然会报错。

// MyException.java文件
public class MyException extends Exception {
    private String msg;
    MyException() {
    }
    MyException(String msg) {
        this.msg = msg;
    }
    @Override
    public String toString() {
        return "MyException{" +
                "msg='" + msg + '\'' +
                '}';
    }
}
// MyRuntimeException.java文件
public class MyRuntimeException extends RuntimeException{
    private String msg;
    MyRuntimeException() {
    }
    MyRuntimeException(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "MyRuntimeException{" +
                "msg='" + msg + '\'' +
                '}';
    }
}
// Application.java文件
public class Application {
    public static void main(String[] args) {
        getAge(151);
        getName("");
    }
    public static void exceptionHandle(int age) throws MyException {
        if (age < 0 || age > 150) {
            throw new MyException(age + "");
        }
    }
    public static void runtimeExceptionHandle(String name) {
        if (name.equals("")) {
            throw new MyRuntimeException("请输入姓名");
        }
    }
    public static void getAge(int age) {
        try {
            exceptionHandle(age);
            System.out.println(age);
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
    public static void getName(String name) {
        runtimeExceptionHandle(name);
    }
}

posted on 2021-04-01 10:03  绊夏微凉  阅读(58)  评论(0)    收藏  举报