在代码中使用自定义的异常类,可以对异常进行统一的封装处理。使得整个项目的异常处理更规范、更统一、更优雅。同时,使得日志的记录上更加清晰,便于后续查日志定位问题。
以下为自定义异常类的完整过程:
1、需要自定义一个类,继承自系统的异常类。具体需要什么样类型的异常类,就继承自相应的系统类。
- 如果希望写一个检查性异常类,则需要继承 Exception 类。
如果你想写一个运行时异常类,那么需要继承 RuntimeException 类。
public class InvalidReqException extends Exception {
private final String userErrMsg;
private final ErrorCode errorCode;
public InvalidReqException(ErrorCode errorCode, String sysErrMsg, String userErrMsg) {
super(sysErrMsg);
this.userErrMsg = userErrMsg;
this.errorCode = errorCode;
}
public InvalidReqException(ErrorCode errorCode, String sysErrMsg, String userErrMsg, Throwable cause) {
super(sysErrMsg, cause);
this.userErrMsg = userErrMsg;
this.errorCode = errorCode;
}
public InvalidReqException(ErrorCode errorCode, String sysErrMsg) {
super(sysErrMsg);
this.userErrMsg = null;
this.errorCode = errorCode;
}
public ErrorCode getErrorCode() {
return errorCode;
}
public String getUserErrMsg() {
return userErrMsg;
}
public String getSysErrMsg() {
return getMessage();
}
}
2、在需要抛出该异常的类或方法中,定义当出现异常时抛出自定义的异常。
static void checkTemplateValid(String content) throws InvalidReqException {
if (content.length() > 100 * 1024) {
throw new InvalidReqException(ErrorCode.TEMPLATE_INVALID, "template is too large, over 100KB",
"模板HTML原文不得超过100KB");
}
}
3、在使用到该类或方法的地方捕捉该异常(或继续抛出,交给上级去处理),并对异常进行业务相关的响应与处理。
public void onMessage(Message message) {
String tenantId = (String) message.getMessageProperties().getHeaders().get(MQRouting.HEADER_TENANT_ID);
String requestId = (String) message.getMessageProperties().getHeaders().get(MQRouting.HEADER_REQ_ID);
String body = null;
CertApplyMsg msg;
try {
body = new String(message.getBody(), "utf8");
log.debug("received cert apply message: {}", body);
msg = JsonUtil.parse(body, CertApplyMsg.class);
} catch (IOException e) { // impossible to happen
log.error("message failed due to invalid format:" + body, e);
failMsg(MQRouting.EXCH_CERT, MQRouting.RK_CERT_APPLY_FAILED, message, e);
return;
}
LegalEntityCertBo entity = msg.getLegalEntityCert();
try {
if (entity.getCert().getStatus() != CertStatus.APPLYING) {
reconcileConcurrentUpdate(tenantId, requestId, msg); // due to retry
return;
}
LegalEntityCertBo created = certService.applyCert(entity.getOrganization(),
entity.getCert().getCa(), entity.getLegalEntity());
if (!certService.updateCert(created, entity.getCert().getStatus())) { // due to data race
reconcileConcurrentUpdate(tenantId, requestId, msg);
} else {
enqueueCallback(tenantId, requestId, msg, created, null, null);
log.info("successfully processed cert application:" + body);
}
} catch (InvalidReqException e) {
log.error("applying cert rejected. body=" + body + ", message=" + message, e);
enqueueCallback(tenantId, requestId, msg, entity, e.getErrorCode(), e.getSysErrMsg());
rejectMsg(MQRouting.EXCH_CERT, MQRouting.RK_CERT_APPLY_REJECTED, message, e);
} catch (RuntimeException e) {
log.error("cert application failed, probably will retry, body=" + body + ", msg=" + e.getMessage(), e);
throw e;
}
}
