博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义异常三
阅读量:7221 次
发布时间:2019-06-29

本文共 4739 字,大约阅读时间需要 15 分钟。

自定义异常三-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 message31      * @param params32      * @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     @Override69     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     @Override35     public String getSuperCode() {36         String code = super._CODE+_CODE;37         return StringUtils.substringBeforeLast(code, DOT);38     }39     40     41     42     43 }
View Code

 

转载地址:http://eyeym.baihongyu.com/

你可能感兴趣的文章
LibreBoard —— Trello 的开源版本(Node.js)
查看>>
《计算机系统:系统架构与操作系统的高度集成》——2.1 处理器设计涉及什么...
查看>>
没有开玩笑:云计算之后是……雾计算
查看>>
不重视 TDD 与 Code Review 的代价
查看>>
《现代体系结构上的UNIX系统:内核程序员的对称多处理和缓存技术(修订版)》——2.13 习题...
查看>>
《产品设计与开发(原书第5版)》——1.2 谁来设计和开发产品
查看>>
Gartner:75%的App无法通过明年标准安全测试
查看>>
《文明之光 第一册》一一7.3 第二节 最珍贵的财富(1)
查看>>
《树莓派Python编程入门与实战》——3.8 使用适当的工具
查看>>
使用云市场快速搭建小型电商网站
查看>>
如何使用数据库12.2简化数据验证代码?
查看>>
《DevOps:软件架构师行动指南.》导读
查看>>
《应用程序性能测试的艺术(第2版)》—第2章 2.3节性能测试工具集:概念验证...
查看>>
《HTML5 开发实例大全》——1.7 实现下拉弹出效果
查看>>
《Python数据可视化编程实战》—— 1.1 介绍
查看>>
《代码整洁之道》—第1章1.7节前传与原则
查看>>
精通Python网络爬虫:核心技术、框架与项目实战.2.1 网络爬虫技能总览图
查看>>
《Spark大数据分析:核心概念、技术及实践》一 第2章 Scala编程
查看>>
《Puppet实战手册》——1.10 利用Git钩子自动进行语法检查
查看>>
《解读NoSQL》——2.9 小结
查看>>