2021_03_09_异常处理

异常机制

什么是异常

  • 实际情况下,遇到的情况不可能是非常完美的。比如,用户输入不符合规定,文件格式错误,网络连接失败、数据库为空,内存溢出等会出现异常的问题。

  • 异常发生在程序运行期间,会影响正常的程序执行流程。

  • 异常Exception,可能会导致程序崩溃。捕获异常并采取正确的处理方式能让程序变得健壮。

    public class Demo01 {
    
        public static void main(String[] args) {
    
            System.out.println(11 / 0);
        }
    }
    
    
    //Exception in thread "main" java.lang.ArithmeticException: / by zero
    //	at ExceptionDemo.Demo01.main(Demo01.java:7)
    
  • 简单分类:

    • 检查性异常:

      用户错误或者问题引起的异常。这是程序员无法预见的。例如要打开一个不存在文件时。

      这些异常在编译时不能被简单地忽略。

    • 运行时异常

      运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时被忽略。

    • 错误Error

      错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,栈溢出,内存溢出等等。

    • Exception可以通过程序来处理。Error无法处理,只有更改代码来避免。


异常体系结构及处理机制

  • 抛出异常

  • 捕获异常

  • 异常处理五个关键字

    • try,catch,finally(可选)
    • 异常也是类 Throwable -> Error, Exception -> 具体Error或异常
    • catch可以写多个,但是要从小范围到大范围
    public class Demo02 {
        public static void main(String[] args) {
            int a = 1;
            int b = 0;
    
            //try 监控区域
            try {
                System.out.println(a/b);
            }catch (ArithmeticException e) { //捕获异常
                System.out.println("Exception, b can't be zero.");
            }catch (Throwable e) {
                System.out.println(e);
            }finally { //善后处理 无论出不出异常finally一定会被执行
                System.out.println("finally");
    
                //finally一般执行善后工作 比如关闭io流
            }
        }
    }
    // ctrl + alt + t可以直接快捷调出包围代码块选项,其中包含try catch代码块
    
    //Exception, b can't be zero.
    //finally
    
  • throw, throws

    public class Demo03 {
        public static void main(String[] args) throws Exception {
            int a = 1;
            int b = 0;
            if (b == 0) {
                throw new ArithmeticException(); // 主动抛出异常 一般在方法中使用
            }
        }
    }
    
    
    //Exception in thread "main" java.lang.ArithmeticException
    //	at ExceptionDemo.Demo03.main(Demo03.java:8)
    
    public class Demo04 {
        public static void main(String[] args) {
    
            int a = 1;
            int b =0;
            try {
                new Demo04().test(a, b);
            } catch (ArithmeticException e) {
                System.out.println("Error");
            }
    
        }
    
        //throws替方法抛出异常,让上级程序进行处理
        public double test(int a, int b) throws ArithmeticException{
            return a/b;
        }
    }
    
    //Error
    

自定义异常

  • 使用java内置的异常类可以描述在编程时出现的大部分异常情况。除此之外,用户还可以自定义异常。用户自定义异常类只需要继承Exception类即可。

  • 自定义异常的几个步骤

    1. 创建自定义类
    2. 在方法中通过throw关键字抛出异常
    3. 如果在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理;否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作。
    4. 在出现异常方法的调用者中捕获异常并处理
    public class DIYException extends Exception{
        private int detail;
    
        public DIYException(int detail) {
            this.detail = detail;
        }
    
        //alt+insert快捷键能调出toString的编写
        @Override
        public String toString() {
            return "DIYException{" +
                    "detail=" + detail +
                    '}';
        }
    }
    
    public class Demo {
        public static void main(String[] args) {
            //执行的时候使用try catch来捕获异常并输出异常
            try {
                new Demo().test(1);
            } catch (DIYException e) {
                e.printStackTrace();
            }
        }
    
        //使用自定义的抛出异常
        public void test (int a) throws DIYException {
            if (a == 1) {
                throw new DIYException(a);
            }
            System.out.println("test " + a);
        }
    }
    
    //DIYException{detail=1}
    //	at ExceptionDemo.Demo05.Demo.test(Demo.java:15)
    //	at ExceptionDemo.Demo05.Demo.main(Demo.java:6)
    

总结

  • 处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理
  • 在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常。
  • 对于不确定的代码,也可以加上try-catch,处理潜在的异常。
  • 尽量去处理异常,切忌只是简单地调用printStackTrace()去打印输出
  • 具体如何处理异常,要根据不同的业务需求和异常类型去决定。
  • 尽量添加finally语句块去释放占用的资源。
posted @ 2021-03-09 17:42  cutomorrow  阅读(60)  评论(0)    收藏  举报