SSM异常处理

定义系统未知异常,运行时异常,客户使用时异常

自定义异常编码

  • 提供控制或业务层调用,返回给前端或异常处理
public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer SELECT_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR= 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer SELECT_ERR = 20040;

    public static final Integer SYSTEM_UNKNWO_ERR=5900;
    public static final Integer SYSTEM_TIMEOUT_ERR=5910;
    public static final Integer BUSINESS_ERR=5920;
}

自定义定义code码方便与前端交互

1. 系统运行异常

  • 成员变量code,方便后面再最终的全局异常中使用
public class SystemException extends RuntimeException{
    private Integer code;

    public SystemException(Integer code,String message) {
        super(message);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public SystemException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

}

定义了一个名为 SystemException 的类,它继承自 RuntimeException。这个类用于表示系统中的一种异常,其中包含了两个主要特性:一个错误码(code)和一个描述性消息(通过继承自 RuntimeException 的 message)。此外,提供了两个构造函数来创建 SystemException 实例,一个接受错误码和消息,另一个还额外接受一个 Throwable 类型的 cause 参数来表示异常的原因。

2. 用户异常

public class BusinessException extends RuntimeException{
    private Integer code;

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }
}

同上

3. 全局异常处理

  • 集中处理系统异常,运行异常,用户异常向上抛出
@RestControllerAdvice
public class ProjectExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Result doException(Exception exception){
        //记录日志
        //发送消息给运维人员
        //发送邮件给开发人员 发送ex对象
        return new Result(Code.SYSTEM_UNKNWO_ERR,null,"系统繁忙");
    }

    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException exception){
        //记录日志
        //发送消息给运维人员
        //发送邮件给开发人员 发送ex对象
        return new Result(exception.getCode(),null, exception.getMessage());
    }

    @ExceptionHandler({BusinessException.class})
    public Result doBusinessException(BusinessException exception){
        return new Result(exception.getCode(),null,exception.getMessage());
    }

定义了一个名为 ProjectExceptionAdvice 的类,它使用了 @RestControllerAdvice 注解来全局处理控制器中抛出的异常。这个类通过定义多个 @ExceptionHandler 方法来捕获并处理不同类型的异常,包括 Exception(所有异常的基类)、SystemException(自定义的系统异常)和 BusinessException(自定义的业务异常)。

模拟异常:

 controller层模拟异常

  • 此时抛出的是运行异常
@GetMapping("/select")
    public Result selectAll(){
        List<Book> books = bookService.selectAll();

        Integer code = books != null ? Code.SELECT_OK : Code.SELECT_ERR;
        String msg = books != null ? "查询成功" :" 查询失败";

        if (true) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"模拟异常");
        }
        return new Result(code,books,msg);
    }

执行过程:

1. 此时程序执行到异常这里,抛出SystemException这个异常。

2. 向SystemException类传入code异常码,和msg异常信息。给code变量赋值

3. 再被全局异常处理器ProjectExceptionAdvice接收调用此类中,对应的自定义异常处理方法4. doSystemException。得到最开始controller层传入的code异常码和msg异常信息返回出去

  • 前端显示

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值