JAVA基础篇(2)-异常

JAVA && Spring && SpringBoot2.x — 学习目录

异常类

1. Exception异常

1.1 运行时异常

运行时异常(unchecked未经检查的异常)RuntimeException及其子类是未经检查的异常 。运行时异常的特点就是Java编译期不会检查它,也就是说,当程序中可能出现这类异常时,既可以不使用try-catch捕获它,也可以不使用throws字句声明它,也可以编译通过。

如下图:

public void testRuntimeException() {
        throw new RuntimeException("我并不需要捕获或抛出");
    }

1.2 检查时异常

非运行时异常(检查异常):Exception类中除RuntimeException之外的异常。从语法上是必须要进行处理的,不然编译不过去。如IOExceptionSQLException等以及(敲黑板,划重点)用户自定义的Exception异常。出现检查时异常。要么try-catch捕获它,要么throws抛出它。


2. try-catch-finally的关系

2.1 catch里面返回,会执行finally的代码吗?

  public static int testFinally() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            return 1;
        } finally {
            System.out.println("你猜我执行了吗");
        }
        return 2;
    }
 public static void main(String[] args) {
        System.out.println(TestObject.testFinally());
    }
  1. try-catch-finally块中即使含有returnbreakcontinue等改变执行流的关键字,finally也会执行。
  2. finally执行完毕,便执行catch中的return返回。
1输出结果

2.2 try-catch-finally均有返回,到底返回谁?

public static int testFinally() {
        try {
            int i = 1 / 0;
            return 1;
        } catch (Exception e) {
            System.out.println("我是打酱油的,我会不会被输出");
            return 2;
        } finally {
            return 3;
        }
    }

因为finally一定会执行,那么最后返回的一定是finallyreturn。但是只是return返回被覆盖,需要注意的是逻辑代码还是会执行的。

2返回结果

2.3. finally里面返回,catch抛出异常会执行吗。

可以看到,我们抛出的检查时异常,但是并未编译错误。是不是和上面理论有冲突?

image.png

不是的!我们在finally中使用了return,那么catch里面的throw将被忽略。

运行结果:

3运行结果

2.4. finally和catch均抛出异常,到底执行谁?

可以看到,均是抛出的exception异常,但是finally里面需要throws或者捕获,那么最终还是以finally里面的代码为准的。

执行代码

2.5在try-finally中的值传递和引用传递(重点)

  public static int test(int x, int y) {
        int result = x;
        try {
            result = x + y;
            System.out.println("try中的结果:"+result);
            //返回的基本数据类型,在执行finally中,其实是值传递
            return result;
        } finally {
            result = x - y;
            System.out.println("result中的结果:"+result);
        }
    }
    public static void main(String[] args) {
        int x = 3;
        int y = 5;
        int result = test(5, 3);
        System.out.println(result);
    }

返回结果:

try-finally的执行结果

是不是很惊奇!!!

2.6总结及建议

try{}中包含return,那么return时,先将返回结果记录。再执行finally。

  1. 如果finally也有return,则finally中的return会覆盖try中的返回值,否则将记录的结果返回。
  2. 如果finally没有return,若是修改了try{}中的返回值数据,那么return就会被修改,若是基本数据,那么try{ return}不会修改。
    需要注意的是:Java里面是值传递,如果本身是基本数据类型,数据不会收到影响,如果是引用类型,数据会改变。

有点绕...
为了不给后来人挖坑,我们需要记住:

  1. 不要在finally中使用return或者抛出异常。
  2. 减轻finally工作量,不要在finally中做其他的事情,finally最好仅仅用来释放资源最合适。
  3. 尽量不在try-catch-finally里面使用return,将其放在函数最后面。

3. 项目中异常处理

抛出的异常经过try-catch处理之后,若未继续抛出新的异常,那么便像正常程序一样继续向下执行。即执行完毕try-catch不会结束程序。

异常类可保存错误码和错误信息

public class BusinessException extends RuntimeException {
    //继承的顶级父类实现了序列化接口,子类还是需要显示的声明SerialVersionUID。
    private static final long serialVersionUID = -3516896655779527315L;
    /**
     * 保存用户错误码
     */
    private String code = "";
    public BusinessException() {
        super();
    }
    public BusinessException(String message) {
        super(message);
    }
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
    }
    public BusinessException(Throwable cause) {
        super(cause);
    }
    public BusinessException(String code, String message) {
        super(message); //调用父类的this.message=message;
        this.code = code;
    }
    public BusinessException(String code, String message, Throwable cause) {
        super(message, cause); //调用父类的构造方法,减少重复代码
        this.code = code;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
}

参考:
Java异常总结

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。