SpringBoot全局异常处理加自定义异常类

一、创建全局异常处理类

package com.example.springbootdemo.common.conf.exceptions;

import com.example.springbootdemo.common.response.ResultBean;
import com.example.springbootdemo.common.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: GlobalExceptionHandler
 * @Description: 全局异常处理
 * @Author: TanXJ
 * @Date: 2023/2/7 9:56
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public ResultBean bizExceptionHandler(HttpServletRequest req, CustomException e){
        log.error("发生业务异常!原因是:{}",e.getMessage());
        return ResultBean.fail(e.getCode(),e.getMessage());
    }

    /**
     * 处理运行时异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public ResultBean runtimeExceptionHandler(HttpServletRequest req, RuntimeException e){
        log.error("发生运行时异常!原因是:{}",e.getMessage());
        return ResultBean.fail(500, e.getMessage());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResultBean exceptionHandler(HttpServletRequest req, NullPointerException e){
        log.error("发生空指针异常!原因是:",e);
        return ResultBean.fail(ResultCode.SYSTEM_ERROR);
    }


    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultBean exceptionHandler(HttpServletRequest req, Exception e){
        log.error("未知异常!原因是:",e);
        return ResultBean.fail(ResultCode.UNKNOWN_ERROR);
    }
}

二、创建自定义异常类

package com.example.springbootdemo.common.conf.exceptions;

import com.example.springbootdemo.common.response.ResultCode;
import lombok.Getter;

/**
 * @ClassName: CustomException
 * @Description: 自定义异常类
 * @Author: TanXJ
 * @Date: 2022/9/4 16:58
 */
@Getter
public class CustomException extends RuntimeException {
    private Integer code;
    private String msg;

    public CustomException() {
        super();
    }

    public CustomException(ResultCode resultCode) {
        super(resultCode.getMessage());
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
    }

    public CustomException(ResultCode resultCode, Throwable cause) {
        super(resultCode.getMessage(), cause);
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
    }

    public CustomException(String errorMsg) {
        super(errorMsg);
        this.msg = errorMsg;
    }

    public CustomException(Integer errorCode, String errorMsg) {
        super(errorMsg);
        this.code = errorCode;
        this.msg = errorMsg;
    }

    public CustomException(Integer errorCode, String errorMsg, Throwable cause) {
        super(errorMsg, cause);
        this.code = errorCode;
        this.msg = errorMsg;
    }
    public Integer getErrorCode() {
        return code;
    }

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

    public String getErrorMsg() {
        return msg;
    }

    public void setErrorMsg(String errorMsg) {
        this.msg = errorMsg;
    }

    public String getMessage() {
        return msg;
    }

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }
}

三、测试全局异常处理效果

在这里插入图片描述

在这里插入图片描述

四、 测试自定义异常处理效果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 中,可以通过全局异常处理机制来统一处理应用程序中的异常。全局异常处理可以捕获应用程序中抛出的异常,并根据需要进行处理和响应。 下面是一种实现全局异常处理的方式: 1. 创建一个自定义异常处理,可以命名为 `GlobalExceptionHandler`。 ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception ex) { // 根据需要进行异常处理逻辑 // 可以返回自定义错误信息或错误码等 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred"); } } ``` 2. 使用 `@ControllerAdvice` 注解标记该全局异常处理。 3. 使用 `@ExceptionHandler` 注解来指定需要处理的异常型,可以通过传入 `Exception.class` 来处理所有型的异常。 4. 在 `handleException` 方法中,可以根据需要编写异常处理逻辑。可以返回自定义错误信息或错误码等。 在上述示例中,`handleException` 方法会捕获所有型的异常,并返回一个带有错误信息的 `ResponseEntity` 对象。 您可以根据实际需求在全局异常处理中定义多个方法,每个方法处理不同型的异常。这样可以根据不同的异常型,提供不同的错误处理逻辑。 请注意,在实际应用中,您可能还需要使用其他注解,如 `@RestControllerAdvice` 或 `@ResponseBody`,以确保正确地处理异常并返回适当的响应。 通过实现全局异常处理,您可以统一处理应用程序中的异常,并提供一致的错误响应。这有助于提高代码的可维护性和用户体验。 希望对您有所帮助!如果还有其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值