ErrorCode.java 简单测试代码,具体应用思路:手动抛出异常信息,在事务中根据错误码来回滚事务的思路。
public enum ErrorCode {
//系统级
SUCCESS("000000","success"),
SYS_ERROR("999999","系统异常"),
FAILED("900000","操作失败!"),
//交易部分
OWNER_NOT_EXIST("500002","用户(车主)不存在"),
; private String code;
private String text; private ErrorCode(String code, String text) {
this.code = code;
this.text = text;
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getText() {
return text;
} public void setText(String text) { this.text = text;
} public static void main(String[] args) { ErrorCode error = ErrorCode.FAILED;
try {
error = testFun(100); //测试方法。
} catch (Exception e) {
System.err.println("e: " + e);
String errorMsg = e.getMessage();
System.err.println("errorMsg: " + errorMsg);
System.err.println("errorMsg2: " + e.getLocalizedMessage()); if(errorMsg != null){
String[] msg = errorMsg.split(",");
if(msg != null && msg.length > 0){
ErrorCode.SYS_ERROR.setCode(msg[0]);
ErrorCode.SYS_ERROR.setText(msg[1]);
}
}
//返回错误码
error = ErrorCode.SYS_ERROR;
System.err.println("code: " + error.getCode() );
System.err.println("text: " + error.getText() ); } } private static ErrorCode testFun(int i) throws Exception {
System.err.println("接收到的参数值:" + i);
ErrorCode error = ErrorCode.OWNER_NOT_EXIST;
String errorMsg = error.getCode() + "," + error.getText(); //将异常信息返回。
throw new Exception(errorMsg); //手动抛出异常。
// throw new Exception(); //手动抛出异常。
//return null;
}
}