自定义异常三-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 }