当前位置: 代码网 > it编程>编程语言>Java > springBoot项目中的全局异常处理和自定义异常处理实现

springBoot项目中的全局异常处理和自定义异常处理实现

2024年08月12日 Java 我要评论
1 什么是异常异常(exception)是在程序执行过程中出现的一种特殊情况或错误。它可以是由于程序逻辑错误、运行环境问题、用户输入错误等原因导致的一种非正常的状态或事件。在编程领域中,异常通常用来表

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项目中的全局异常处理和自定义异常处理实现的文章就介绍到这了,更多相关springboot 全局异常和自定义异常内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com