springboot 自定义异常类 全局异常处理机制

376 阅读2分钟

关于全局异常处理机制

大家好啊,我是你们的老朋友杨洋,最近呢,在搭建小组的jenkins自动发布、准备新项目的activiti工作流搭建,比较忙,之前还差了几篇文章就后面再补上了,今天呢跟大家聊一下全局自定义异常处理机制

全局异常处理机制

定义一个全局自定义异常

package com.yang.demo.exception;

import com.yang.demo.service.BaseErrorInfoInterface;

/**
 * 整个项目使用的抛异常的类,用来约束整个项目
 *
 * @author yanglei
 * @date 2020/8/3
 */
public class DemoException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    protected String errorCode;
    /**
     * 错误信息
     */
    protected String errorMsg;

    public DemoException() {
        super();
    }

    public DemoException(BaseErrorInfoInterface errorInfoInterface) {
        super(errorInfoInterface.getResultCode());
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

    public DemoException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
        super(errorInfoInterface.getResultCode(), cause);
        this.errorCode = errorInfoInterface.getResultCode();
        this.errorMsg = errorInfoInterface.getResultMsg();
    }

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

    public DemoException(String errorCode, String errorMsg) {
        super(errorCode);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public DemoException(String errorCode, String errorMsg, Throwable cause) {
        super(errorCode, cause);
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }


    public String getErrorCode() {
        return errorCode;
    }

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

    public String getErrorMsg() {
        return errorMsg;
    }

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

    @Override
    public String getMessage() {
        return errorMsg;
    }

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

  • 一般抛出的自定义异常都是继承RuntimeException

设定每个try catch的异常抛出

@Override
    public int insertMenuInfo(MenuDto menuDto) {
        try {
            int insertMenuNum = menuMapper.insertMenu(menuDto);
            int a = 10 / 0;
            return insertMenuNum;
        } catch (Exception e) {
            throw new DemoException("出错啦");
        }

    }

全局异常处理类

package com.yang.demo.exception;

import com.yang.demo.model.Response;
import com.yang.demo.model.enumModel.CommonEnum;
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;

/**
 * 全局异常处理的类
 *
 * @author yanglei
 * @date 2020/8/3
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

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

    /**
     * 处理自定义的业务异常
     * @param req 无用
     * @param e 异常
     * @return
     */
    @ExceptionHandler(value = DemoException.class)
    public Response<String> bizExceptionHandler(HttpServletRequest req, DemoException e){
        logger.error("发生业务异常!原因是:{}",e.getErrorMsg());
        return Response.error(e.getErrorCode(),e.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     * @param req 无用
     * @param e 异常
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    public Response<String> exceptionHandler(HttpServletRequest req, NullPointerException e){
        logger.error("发生空指针异常!原因是:",e);
        return Response.error(CommonEnum.BODY_NOT_MATCH);
    }


    /**
     * 处理其他异常
     * @param req 无用
     * @param e 异常
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    public Response<String> exceptionHandler(HttpServletRequest req, Exception e){
        logger.error("未知异常!原因是:",e);
        return Response.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

}

那么到这边呢,大家就实现了全局异常处理的实现,这边呢,注意点是:

  1. 自定义异常一般继承RuntimeException,当然你指定别的也行
  2. 全局报错、逻辑等的返回直接使用这个自定义异常
  3. 自定义异常处理的类要注意的是@RestControllerAdvice

那么,今天的分享就到这里了啊,后面我会推出jenkins+svn实现springboot多模块项目的自动部署(jar包部署到另一台服务器,斥巨资的两台服务器),再晚一点推出springboot2.0.4+activiti6.0的整合,大家多多关注我噢~那么,掰掰