参考文档

基础业务异常类

import com.anchi.car.coresystem.consumer.common.ResponseCode;

/**
 * @author: shuaige
 * @description: 基础业务异常类
 * @date: 2019/3/3 9:08
 */
public class BaseBusinessException extends RuntimeException {

    private static final long serialVersionUID = -5679241599209889046L;
    private Integer code;

    public BaseBusinessException() {
    }

    /**
     * 给子类用的方法
     *
     * @param responseCode
     */
    public BaseBusinessException(ResponseCode responseCode) {
        this(responseCode.getDesc(), responseCode.getCode());
    }

    public BaseBusinessException(String message) {
        super(message);
    }

    private BaseBusinessException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

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

示例——登录类

import com.anchi.car.coresystem.consumer.common.ResponseCode;

/**
 * 登录信息异常
 *
 * @author: shaoshuaui.zheng
 * @description:
 * @date: 2019/3/3 9:16
 */
public class LoginException extends BaseBusinessException {

    private static final long serialVersionUID = 8089598027182669379L;

    public LoginException() {
    }

    public LoginException(String message) {
        super(message);
    }

    /**
     * 给子类用的方法
     *
     * @param responseCode
     */
    public LoginException(ResponseCode responseCode) {
        super(responseCode);
    }
}

使用示例

/**
 * 登录异常
 *
 * @param ex 异常
 */
@ExceptionHandler({LoginException.class})
public ServerResponse<Object> businessExceptionHandler(LoginException ex) {
    this.log.error(ex);
    return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), "login error");
}

抛出异常

if (StringUtils.isBlank(cookieValue)) {
    throw new LoginException("登录异常");
}