Java 封装全局异常类

时间:2025-04-05 22:30:14

今天学习Java第五天,写demo的时候捕获异常,很不习惯,毕竟PHP可以很方便直观的处理,查查资料,自己写了一个,刚开始学,写的有点LOW

1.全局异常处理类

import ;

import ;
import ;
import ;
import ;
import ;

import ;

import ;
import ;

/**
 * ClassName:全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @Autowired
    private Environment environment;
    @ExceptionHandler(value = )
    @ResponseBody
    public JsonReturn  defaultErrorHandler(HttpServletRequest request, Exception ex)
            throws Exception {
        String errorCode = null;
        String errorMsg = null;
        Object errorData = new ArrayList();
        String active = ("");
        if (ex instanceof BusinessException) {
            BusinessException se = (BusinessException) ex;
            errorCode = ();
            errorMsg = ();
            errorData = ();
            // 屏蔽线上异常
        } else if(active == "prod"){
            errorCode = "0";
            errorMsg = "内部服务错误";
        } else {
            ();
        }
         return  new JsonReturn(errorCode,errorMsg, errorData);
    }


}

2.创建一个异常文件  exception

定义一个继承runtime异常的类,BaseRuntimeException

import ;

import ;


public class BaseRuntimeException extends RuntimeException  {
    private Object errorData;
    private Throwable cause = null;
    private String errorCode = null;

    private String errorMsg = ""; // 客户描述
    private String alarmMsg = "!@#$"; // 告警描述

    static final long serialVersionUID = 0L;

    public BaseRuntimeException() {
    }

    public BaseRuntimeException(String errorCode) {
         = errorCode;
    }

    /**
     * 透传其他模块的错误信息 Creates a new instance of BaseRuntimeException.
     *
     * @param errorCode
     * @param msg
     */
    public BaseRuntimeException(String errorCode, String msg) {
        super(msg);
         = msg;
         = errorCode;
        ();
        ();
    }

    /**
     * 透传其他模块的错误信息并告警 Creates a new instance of BaseRuntimeException.
     *
     * @param errorCode
     * @param msg
     */
    public BaseRuntimeException(String errorCode, String msg, String alarmMsg) {
        super(msg);
         = msg;
         = alarmMsg;
         = errorCode;
    }

    public BaseRuntimeException(Throwable cause) {
        super(cause);
    }

    public BaseRuntimeException(String errorCode, String msg, Throwable cause) {
        super(msg);
         = cause;
         = errorCode;
    }
    public BaseRuntimeException(String errorCode, String msg, Object data) {
        super(msg);
         = data;
         = errorCode;
    }

    @Override
    public Throwable getCause() {
        return ;
    }

    public String getExceptionMessage() {
        if (() != null) {
            return ();
        }
        if ( != null) {
            return ();
        }
        return null;
    }


    public String getErrorCode() {
        return ;
    }

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

    public String getErrorMsg() {
        return errorMsg;
    }

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

    public String getAlarmMsg() {
        return alarmMsg;
    }

    public void setAlarmMsg(String alarmMsg) {
         = alarmMsg;
    }

    public boolean isAlarm() {
        if ((!()) && (!("!@#$"))) {
            return true;
        }
        return false;
    }

}
 

3.定义全局返回

import ;

/**
 * 全局错误码
 * Created by LY on 2018/04/11.
 */
public enum GlobalErrorCode{
    // Login error

    LOGIN_ERROR_TOKEN_INVALID("-10000", "无效的token.", new ArrayList<>()),
    /**
     * 参数类错误码
     */
    REQUEST_PACKET_ERROR("400010","请求数据错误!"),
    TCP_SYSTEM_CONNECT_ERROR("400012","连接数据服务器失败!"),
    REQUEST_PARSE_PACKET_ERROR("400015","服务器处理数据失败!"),

    REQUEST_DB_ADD_ERROR("30001","数据库添加处理失败!"),

    REQUEST_DB_SAVE_ERROR("30002","数据库修改处理失败!"),

    REQ_PARAM_TOKEN_NOTNULL("400014","token参数为空!"),

    REQ_PARAM_TTL_INVALID("-10000","登陆超时!");

    GlobalErrorCode(String code,String message){
        = code;
        = message;
    }
    GlobalErrorCode(String code,String message, Object data){
        = code;
        = message;
        = data;
    }

    private String code;
    private String message;
    private Object data;


    public String getCode() {
        return ;
    }


    public String getMessage() {
        return ;
    }


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        = data;
    }
}
 

4.错误捕捉

import ;

public class BusinessException extends BaseRuntimeException {

    private static final long serialVersionUID = 1L;

    //自定义错误码
    private String code;
    private Object data;

    //自定义构造器,只保留一个,让其必须输入错误码及内容
    public BusinessException(String code, String msg) {
        super(code, msg);
        = code;
    }

    public BusinessException(String code, String msg, Object data) {
        super(code, msg, data);
        = code;
        = data;
    }

    public BusinessException(GlobalErrorCode globalErrorCode) {
        super((), (), ());
        = ();
        = ();
    }


    public String getCode() {
        return ;
    }

    public void setCode(String code) {
        = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        = data;
    }
}
 

基本以上四个可以拿去直接使用