Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wordpress-seo domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/wp-includes/functions.php on line 6114 如何搭建springBoot项目中的全局异常处理和自定义异常处理 - 说了一半的话的博客
如何搭建springBoot项目中的全局异常处理和自定义异常处理

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 测试异常处理

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

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

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


本文《如何搭建springBoot项目中的全局异常处理和自定义异常处理》由 说了一半的话 发布。想了解更多详细内容,请点击 这里


欢迎分享这篇文章链接:https://odgclusterflutter.top/%e5%a6%82%e4%bd%95%e6%90%ad%e5%bb%baspringboot%e9%a1%b9%e7%9b%ae%e4%b8%ad%e7%9a%84%e5%85%a8%e5%b1%80%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e5%92%8c%e8%87%aa%e5%ae%9a%e4%b9%89%e5%bc%82%e5%b8%b8%e5%a4%84/



暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇