如何搭建springBoot项目中的全局异常处理和自定义异常处理

目录

1 什么是异常

2 异常的种类

3 解决异常的方式

4 全局异常处理器和自定义异常处理器

5 测试异常处理


1 什么是异常

异常(Exception)是在程序执行过程中出现的一种特殊情况或错误。它可以是由于程序逻辑错误、运行环境问题、用户输入错误等原因导致的一种非正常的状态或事件

在编程领域中,异常通常用来表示一种无法预料或处理的情况,它会导致程序无法继续正常执行。当程序运行过程中遇到异常,如果没有适当的处理机制,可能会导致程序崩溃或产生未预期的结果。

2 异常的种类

Checked Exception(已检查异常):这类异常是在编译时强制要求程序员进行处理的异常,如文件未找到、数据库连接失败等。Java 中的 checked exception 必须在方法签名中声明,或者使用 try-catch 块进行捕获处理,否则编译不通过。

Unchecked Exception(未检查异常):也称为运行时异常(RuntimeException),通常是由于编程错误或者运行环境出现问题引起的异常,如空指针异常(NullPointerException)、数组越界异常(ArrayIndexOutOfBoundsException)等。编译器不要求强制捕获或者声明这类异常,程序员可以选择捕获和处理,但不是必须的

处理异常的主要目的是为了保证程序的稳定性和可靠性。通常的处理方式包括捕获异常、记录异常信息、恢复程序状态、通知用户等。

3 解决异常的方式

1、捕获和处理异常(try-catch)

  • 使用 try-catch 块来捕获可能抛出异常的代码块。
  • try 块中编写可能引发异常的代码,然后在 catch 块中处理异常。
  • 这种方式适用于已检查异常(checked exception),以及可以预料到可能出现的异常情况

2、抛出异常(throw)

  • 在方法中使用 throw 关键字抛出异常,告知调用者可能发生的异常情况。
  • 通常在方法内部检测到无法处理的情况时抛出异常,将问题交给上层调用者处理。

3、使用 finally 块

  • finally 块中的代码总是会被执行,无论是否抛出异常,用于释放资源或者确保某些操作一定会执行。

4、使用 try-with-resources

  • 对于实现了 AutoCloseable 接口的资源,可以使用 try-with-resources 语句,确保资源在使用后自动关闭。
  • 适用于需要手动关闭的资源管理,如文件操作、数据库连接等。

4 全局异常处理器和自定义异常处理器

全局异常处理器是一种集中处理应用程序中所有未捕获异常的机制,常用于 Web 应用程序中,以提供统一的异常处理和用户友好的错误响应。在 Java 的 Spring 框架中,可以使用 @ControllerAdvice @ExceptionHandler 注解来实现全局异常处理。在全局异常处理器中,解决了异常种类繁多以及每个业务里面都需要去处理异常,所以Spring中提供了一种上层调用的方式来进行异常处理,把数据层和业务层的异常都向上抛放到应用层来处理,这样就可以统一对异常进行处理

类:ProjectExceptionAdvice

package com.hyh.ad.common.exceptionhandler;

import cn.hutool.core.date.DateTime;
import com.hyh.ad.common.core.domain.AjaxResult;
import com.hyh.ad.common.exceptions.BusinessException;
import com.hyh.ad.common.exceptions.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 异常处理切面
 * 处理BusinessException异常
 * 处理SystemException异常
 * @author hyh
 */

@Slf4j
@RestControllerAdvice
public class ProjectExceptionAdvice {
    /**
     * @param  e
     * @return 业务异常处理的信息
     */
    @ExceptionHandler(BusinessException.class)
    public AjaxResult handleBusinessException(BusinessException e) {
        //记录日志
        log.error(e.getMessage(), e);
        //返回错误信息
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /**
     * @param  e
     * @return 系统异常处理的信息
     */
    @ExceptionHandler(SystemException.class)
    public AjaxResult handleSystemException(BusinessException e) {
        //记录日志
        log.error(e.getMessage(), e);
        //返回错误信息
        return AjaxResult.error(e.getCode(), e.getMessage());
    }

    /** 
     * 自定义异常处理的信息
     * 其他异常都经过这个来处理 也可以自己来定义自己需要的异常处理
     * @param  e
     * @return 
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult doException(Exception e) {
        //记录日志
        log.error(e.getMessage(), e);
        //返回错误信息
        return AjaxResult.error(e.getMessage() + DateTime.now());
    }
}

其中AjaxResult请换成自己封装的用于返回数据结果的对象 

在这个类里面可以对所有的异常进行处理,包括系统异常、编译异常、运行异常、自定义异常这些异常返回相对友好的信息。

类:BusinessException 业务异常

package com.hyh.ad.common.exceptions;


/**
 * 业务异常处理器
 * @author hyh
 */
public class BusinessException extends RuntimeException{

    /*
     * 异常消息
     */
    private Integer code;

    public Integer getCode() {
        return code;
    }

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

    public BusinessException(Integer code) {
        this.code = code;
    }

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

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

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

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

 类:SystemException 系统异常

package com.hyh.ad.common.exceptions;
/**
 * 系统异常处理器
 * @author  hyh
 */
public class SystemException extends RuntimeException{

    /*
     * 异常消息
     */
    private Integer code;

    public Integer getCode() {
        return code;
    }

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

    public SystemException(Integer code) {
        this.code = code;
    }

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

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

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

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

5 测试异常处理

 

这里就是我模拟的一个发送邮箱验证码的异常,他把异常信息返还给了前端,并且还带有异常发生的时间。

接下来 我手动模拟了一个异常 然后再次请求一下数据

可以看到 返回了我们自定义的异常信息,包括提示信息和编码 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值