SpringBoot自定义异常

为什么要自定义异常呢?

已有的异常,我们无法更改它的信息(比如我们无法添加一个异常code),并且已有的异常是所有人都在用的异常,我们不好对它进行拦截特殊处理。


一、自定义异常

其实异常的种类,我们无法去自定义已经有了,比如Exception,RuntimeExceprion等,我去继承这些类,来实现我们的自己的异常,继承了RuntimeException本质上也就是RuntimeException这种异常了

1-1、BizException
public class BizException extends RuntimeException{

    private Integer errCode;

    private String errMsg;

    public BizException(Integer errCode, String errMsg, Exception e){
        super(e);
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public Integer getErrCode() {
        return errCode;
    }

    public void setErrCode(Integer errCode) {
        this.errCode = errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
}
1-2、使用我们的自定义异常

我们直接抛出就可以了

 @GetMapping("/exception")
 public void fun(){
     try {
         int i = 1 / 0;
     }catch (Exception e){
         throw new BizException(500,"系统异常",e);
     }
 }

二、异常处理器

其实这个很好理解,就是当发生了这种异常,就会进到这个处理器进行处理。

具体怎么处理,我们可以由自己的业务逻辑进行处理。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class BizEceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(BizEceptionHandle.class);

    @ExceptionHandler({BizException.class})
    public String exec(HttpServletRequest request, BizException e){

        // 日志记录
        logger.error(request.getRequestURI(),e);

        // 给开发者发送一个消息
        e.printStackTrace();

        // 异常处理
        return e.getErrMsg();
    }
}

三、其它

一般我们开发都会是封装一个统一返回值给前端的,所以上面的这个exec方法的返回值,我们也可以用我们统一的返回值,因为这个返回值将返回给前端(需要说明的是,即便的异常处理器的返回值和你的方法返回值不同也没有关系,最后以异常处理器为准,但是最好是一致的)


四、B站视频

SpringBoot胡咧咧,文件上传、Excel、HttpClient、日志、异常

https://www.bilibili.com/video/BV1kf4y1i761?p=10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot中,我们可以通过自定义异常来处理业务逻辑中出现的异常情况。以下是实现步骤: 1. 自定义异常 在项目包中创建一个自定义的异常,需要继承Exception或RuntimeException。 ``` public class CustomException extends RuntimeException { private int code; private String message; public CustomException(int code, String message) { this.code = code; this.message = message; } public int getCode() { return code; } public String getMessage() { return message; } } ``` 2. 异常处理 在项目包中创建一个异常处理,需要使用@ControllerAdvice和@ExceptionHandler注解,通过指定异常来捕获异常并处理。 ``` @ControllerAdvice public class CustomExceptionHandler { @ExceptionHandler(CustomException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public Result handleCustomException(CustomException e) { return new Result(e.getCode(), e.getMessage(), null); } } ``` 3. 统一响应对象 在项目包中创建一个统一响应对象,用于统一封装响应信息。 ``` public class Result<T> { private int code; private String message; private T data; public Result(int code, String message, T data) { this.code = code; this.message = message; this.data = data; } public int getCode() { return code; } public String getMessage() { return message; } public T getData() { return data; } } ``` 以上就是springboot自定义异常的实现步骤。在业务逻辑中抛出自定义异常即可触发异常处理中的处理方法,并返回统一响应对象。这样可以统一处理异常并返回规范化的响应信息,方便前端或其他系统的处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值