自定義異常三

Love Lenka發表於2016-10-11

自定義異常三-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 }
View Code

自定義異常三-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 }
View Code

實現類

 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 }
View Code

 

相關文章