1. 异常类
自定义异常类定义:
1. 继承 java.lang.Exception
2. 继承 java.lang.RuntimeException,推荐,不需要抛出异常,配合springboot全局异常处理可以返回前端异常信息
2. 自定义异常类
public class XftSignException extends Exception {
public XftSignException(String errorCode, String errorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
public XftSignException(Throwable ex) {
super(ex);
this.errorCode = "000001";
this.errorMessage = ex.getMessage();
}
/**
* 错误编号
*/
private String errorCode;
/**
* 错误消息
*/
private String errorMessage;
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
@Override
public String toString() {
return String.format("XftSignException异常:错误码:%s,错误信息:%s", this.errorCode, this.errorMessage);
}
}
3. 问题分析
这是在对接薪福通跳转第三方应用时,引入的包内XftSignException 异常类
try {
return client.verifySign(data, sign);
} catch (XftSignException e) {
throw new BizException("签名异常", e);
}catch (ApiException e) {
throw new BizException("API请求异常", e);
}
这里 发生 XftSignException 异常了,打印 Caused by: com.yl.base.exception.XftSignException: null
希望打印 e.getMessage() 内容,实际上并没有
问题在定义异常类构造器中,必须给super构造器传入errorMessage值,这样e.getMessage()才能获取到
public XftSignException(String errorCode, String errorMessage) {
super(errorMessage);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
public XftSignException(Throwable ex) {
super(ex.getMessage(), ex);
this.errorCode = "000001";
this.errorMessage = ex.getMessage();
}
或者 不打印 异常对象e, 直接打印异常信息字符串,log.error("出现异常:{}", e.toString());
也可以获取异常信息后,用其他自定义异常类抛出
if (e instanceof XftSignException) {
XftSignException xftSignException = (XftSignException) e;
throw new BizException(xftSignException.getErrorCode(), xftSignException.getErrorMessage());
}
4. try内有BizException,catch内继续抛出BizException
public List<RewardInfo> findAll(RewardInfo rewardInfo) {
try {
System.out.println("==============有问题的地方==============");
if (true) {
throw new BizException("Error001", "角色不匹配");
}
} catch (Exception e) {
throw new BizException("总的异常", e);
}
return rewardInfoMapper.findAll(rewardInfo);
}
希望抛出异常信息是角色不匹配,结果是总的异常,当然异常信息中 cause by 也会打印角色不匹配
解决
1. 在 catch内判断
catch (Exception e) {
if (e instanceof BizException) {
throw new BizException(e.getMessage(), e);
}
throw new BizException("总的异常", e);
}
2. 在构造内判断
public BizException(String message, Throwable throwable) {
// 针对try 内有BizException, catch 内再次throw BizException,需要打印异常信息是try 内BizException 而不是 catch 内BizException
super(throwable instanceof BizException ? ((BizException) throwable).getErrorMsg() : message, throwable);
errorMsg = message;
errorCode = "3999";
}
public BizException(String code, String message, Throwable throwable) {
// 针对try 内有BizException, catch 内再次throw BizException,需要打印异常信息是try 内BizException 而不是 catch 内BizException
super(throwable instanceof BizException ? ((BizException) throwable).getErrorMsg() : message, throwable);
errorCode = code;
errorMsg = message;
}
BizException类
@Getter
@Setter
public class BizException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String errorCode;
private String errorMsg;
public BizException() {
super();
errorCode = "3999";
errorMsg = "业务逻辑异常";
}
public BizException(String message) {
super(message);
errorCode = "3999";
}
public BizException(String code, String message) {
super(message);
errorCode = code;
errorMsg = message;
}
public BizException(String message, Throwable cause) {
super(message, cause);
errorMsg = message;
errorCode = "3999";
}
public BizException(String code, String message, Throwable throwable) {
errorCode = code;
errorMsg = message;
}
public BizException(String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) {
errorCode = "3999";
errorMsg = message;
}
public BizException(String code, String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) {
writableStackTrace);
errorCode = code;
errorMsg = message;
}
public BizException(Throwable throwable) {
errorCode = "3999";
errorMsg = "业务逻辑异常";
}
public String getErrorCode() {
return errorCode;
}
}