Fork me on GitHub
~~~~~~~~~小喵喵已自动为您屏蔽了广告哦~~~~~~~~~

Java异常

Java异常

异常分类

Throwable是Java中的异常根类,有Error和Exception两个子类

Error是程序无法处理的错误,表示运行应用程序中较严重的问题

Exception是程序本身可以处理的异常。异常处理通常指针对这种类型异常的处理。

捕获异常

异常处理

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            int one = input.nextInt();
            int two = input.nextInt();
            System.out.println(one/two);
        }catch (ArithmeticException e){
            System.out.println("除数不能为0");
            e.printStackTrace();
        }catch (InputMismatchException e){
            System.out.println("输出格式错误,请输入整数");
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("error");
        }finally {
            System.out.println("end");
        }

多重catch块,捕获Exception具体子类的异常,可以在最后加入Exception收尾(防止遗漏)且只能在最后

由于finally的执行机制,在finally中的return语句会覆盖掉try和catch中的return

抛出异常

自定义异常

异常链

posted @ 2021-02-07 15:44  走路带风Test  阅读(64)  评论(0编辑  收藏  举报
Demo
Return Top