自定义异常三-1
1 import java.text.MessageFormat; 2 3 /** 4 * 账户异常类 5 * 6 * 该类定义了一些账户里存在的异常 并且不同的code 对应了不同的异常情况 7 * 8 * 9 */ 10 public class FundAccountException extends RuntimeException { 11 12 private static final long serialVersionUID = 618428211364837565L; 13 14 /** 15 * 不合法余额,比如余额为负 16 */ 17 public static final FundAccountException INVALID_BALANCE = new FundAccountException("invalid_balance"); 18 19 private String message; 20 21 private String code; 22 23 protected FundAccountException(String code) { 24 this.code = code; 25 } 26 27 /** 28 * 实例化账户异常 错误信息:a_message :String(类型) 参数列表:a_params :Object[] (类型) 29 * 30 * @param message 31 * @param params 32 * @return AccountException 账户异常 33 */ 34 public FundAccountException newInstance(String message, Object... params) { 35 FundAccountException e = new FundAccountException(this.code); 36 e.setMessage(message, params); 37 return e; 38 } 39 40 41 public void setMessage(String message, Object... args) { 42 this.message = MessageFormat.format(message, args); 43 } 44 45 public String getCode() { 46 return code; 47 } 48 49 public String getMessage() { 50 return message; 51 } 52 }
自定义异常三-2
抽象类
1 import org.apache.commons.lang3.StringUtils; 2 3 /** 4 * * 5 */ 6 public abstract class BaseException extends RuntimeException { 7 8 private static final long serialVersionUID = 4151720706285185333L; 9 10 protected static final String _CODE="exception."; 11 /** 12 * 错误代码 13 */ 14 private String code; 15 16 /** 17 * 模块 18 */ 19 private String module; 20 21 /** 22 * 错误码对应的参数 23 */ 24 private Object[] args; 25 26 public Object[] getArgs() { 27 return args; 28 } 29 30 public void setArgs(Object[] args) { 31 this.args = args; 32 } 33 34 public BaseException(String module,String code) { 35 this.module = module; 36 this.code = _CODE+code; 37 } 38 39 public BaseException(String module,String code, String message) { 40 super(message); 41 this.module = module; 42 this.code = _CODE+code; 43 } 44 45 public BaseException(String module,String code,String message, Throwable cause) { 46 super(message,cause); 47 this.module = module; 48 this.code = _CODE+code; 49 } 50 51 public BaseException(String module,String code,String message, Throwable cause,Object[] args) { 52 super(message,cause); 53 this.module = module; 54 this.code = _CODE+code; 55 this.args = args; 56 } 57 58 public String getCode() { 59 return code; 60 } 61 62 public abstract String getSuperCode(); 63 64 public String toString(){ 65 return new StringBuilder().append("Exception:["+this.getClass().getName()+"],module:["+StringUtils.defaultString(module)+"],code:["+StringUtils.defaultString(code)+"],message:["+StringUtils.defaultString(getMessage())+"],args:["+StringUtils.join(args, ",")+"]").toString(); 66 } 67 68 @Override 69 public boolean equals(Object obj) { 70 if(obj==null){ 71 return false; 72 } 73 if (this == obj) { 74 return true; 75 } 76 if (obj instanceof BaseException) { 77 BaseException other = (BaseException)obj; 78 return this.getCode().equals(other.getCode()); 79 } 80 return false; 81 } 82 }
实现类
1 /** 2 * 付款会员状态异常 3 * @author sxf 4 * 5 */ 6 public class PaymentMemberStateException extends BaseException{ 7 8 /** 9 * 10 */ 11 private static final long serialVersionUID = -8508340203244322249L; 12 13 protected static final String _CODE="payment.member.state"; 14 15 protected static final String DOT="."; 16 17 public PaymentMemberStateException(String module, String code,String message, Throwable cause, Object[] args) { 18 super(module, _CODE+code, message, cause, args); 19 20 } 21 22 public PaymentMemberStateException(String module, String code,String message, Throwable cause) { 23 super(module, _CODE+code, message, cause); 24 } 25 26 public PaymentMemberStateException(String module, String code,String message) { 27 super(module, _CODE+code, message); 28 } 29 30 public PaymentMemberStateException(String module, String code) { 31 super(module, _CODE+code); 32 } 33 34 @Override 35 public String getSuperCode() { 36 String code = super._CODE+_CODE; 37 return StringUtils.substringBeforeLast(code, DOT); 38 } 39 40 41 42 43 }