springboot @ControllerAdvice处理异常无法正确匹配自定义异常

新手上路,请多包涵

Controller 抛出异常无法匹配:
1、 throw new BasicException();可以匹配;
2、 throw new RuntimeException();无法匹配;

是哪里写错了吗?以下是部分代码

控制器Web接口异常处理类 ExceptionHandler:

    @ExceptionHandler(BasicException.class)  //自定义数据异常
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ResultDataView<?> BasicException(BasicException e) {
        log.error(e.getErrMsg());
        return ResultDataView.failure(e.getErrMsg());
    }

自定义异常类:

public class BasicException  extends RuntimeException {
    private static final long serialVersionUID = -2789674242371703555L;

    protected String errMsg;

    public BasicException() {
    }

    public BasicException(String errMsg) {
        this.errMsg = errMsg;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
}
阅读 2.3k
1 个回答

目测没写专门匹配RuntimeException的方法啊,所以就没有喽

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题