Java异常体系

1 什么是异常

定义:Java异常是Java程序在运行时所产生的错误,使程序产生中断的现象称之为异常。

2 异常的分类

在这里插入图片描述
异常的分类

  • 错误error: 如果程序出现了错误,那么程序只能重新启动解决该错误,然后查看日志是那一块出现问题再去盘算一下逻辑如何进行更改优化。例如OutOfMemoryError。
  • 运行时异常又称为编译时异常,这种异常一定得显示的处理,如果不进行处理编译则不能通过,可以使用try catch进行捕获,也可以使用throws继续抛给上层进行处理。这种异常也是程序员在开发过程中设计逻辑不合理所导致成。例如空指针异常。
  • 非受控异常也称为非运行时异常,这类异常不一定得处理。例如IOException,SQLException
    在这里插入图片描述

3 异常的处理

异常捕获的顺序 :由小到大,先捕获子异常,然后捕获父异常。(在写catch 捕获的时候)
getMessage()和printStackTrace()方法对异常信息进行打印。

什么时候用throws和try catch?
当我们确定异常是什么类型的时候就进行try catch 捕获。
readline()这个异常就应该被抛出,在不同的场景下会被不同的代码调用。

4 常见问题

  1. throw 和 throws 的区别?
  • throw
    是在方法体内,由方法体对其进行语句处理。

    一般用于抛出自定义异常

    一定是执行了某种异常

  • throws
    在方法声明的后面,用于抛出异常,由调用者对异常进行处理。

throws表示异常的一种可能性,并不是会一定产生异常。

  1. final、finally和fianlize的区别?
    fianl 关键字表示所修饰的属性不可被更改,所定义的方法不能被修改,所定义的类不能被继承。

    finally异常处理结束的一个部分,表示一定会执行。

    finalize是Object的一个方法,每个对象都有这个方法,当对象没有被更多指向的时候,该对象将会被垃圾回收机制回收。该方法时由GC自动调用的。

  2. try catch中 retrurn的作用。
    只有在 try 里面通过 System.exit(0) 来退出 JVM 的情况下 finally 块中的代码才不会执行,其他 return 等情况都会执行finally 代码块。
    执行顺序是 try ,catch ,finally (没finally 就执行trycatch外的return)

5 SpringBoot全局异常捕获

通过@RestControllerAdvice注解来捕获全局异常

@ExceptionHandler(value = RuntimeException.class)来指定捕获的Exception各个类型异常 ,这个异常的处理,是全局的,所有类似的异常,都会跑到这个地方处理。

package com.jie.platform.exception;

import com.jie.platform.common.lang.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    // 捕捉shiro的异常
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(ShiroException.class)
    public Result handle401(ShiroException e) {
        return Result.fail("401", e.getMessage(), null);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler(IllegalArgumentException e) throws IOException {
        log.error("Assert异常:-------------->{}", e.getMessage());
        return Result.fail(e.getMessage());
    }

    /**
     * @Validated 校验错误异常处理
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result handler(MethodArgumentNotValidException e) throws IOException {
        log.error("运行时异常:-------------->", e);
        BindingResult bindingResult = e.getBindingResult();
        ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
        return Result.fail(objectError.getDefaultMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    public Result handler(RuntimeException e) throws IOException {
        log.error("运行时异常:-------------->", e);
        return Result.fail(e.getMessage());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值