异常抛出类
2016-04-18 14:36
274 查看
package com.lz.ctsframework.core.support; import java.text.MessageFormat; /** * * <b>类说明:</b>Service层统一抛出的异常 * * <p> * <b>详细描述:</b> * * @author liuhuanchao * @since 2016-04-18 */ public class ServiceException extends RuntimeException { private static final long serialVersionUID = 6514891174875747380L; /** 异常错误码 **/ private String code; /** 异常描述 **/ private String msg; /** 扩展异常描述(包括msg) **/ private String extMsg; /** * ServiceException构造方法,有format字符组 * @param errorCode 错误码 * @param param format字符组 * @param extMsg 扩展信息,给出具体的错误值信息等 */ public ServiceException(ErrorCode errorCode,String param[],String ... extMsg) { super(null==errorCode ? "" : errorCode.getCode()); init(errorCode, param,extMsg); } /** * ServiceException构造方法,有format字符组 * @param errCode * @param paramsList */ public ServiceException(ErrorCode errCode, Object... paramsList) { Object[] params = null; if ((paramsList != null) && (paramsList.length > 0) && ((paramsList[(paramsList.length - 1)] instanceof Throwable))) { Object[] newParam = new Object[paramsList.length - 1]; System.arraycopy(paramsList, 0, newParam, 0, newParam.length); params = newParam; super.initCause((Throwable)paramsList[(paramsList.length - 1)]); } else { params = paramsList; super.initCause(null); } this.code = null==errCode ? "" : errCode.getCode(); this.msg = null==errCode ? "" : MessageFormat.format(errCode.getMsg(),params); this.extMsg = this.msg; } private void init(ErrorCode errorCode, String param[], String... extMsg) { this.code = null==errorCode ? "" : errorCode.getCode(); this.msg = null==errorCode ? "" : MessageFormat.format(errorCode.getMsg(),param); StringBuilder builder = new StringBuilder(100); builder.append(this.msg); if(null != extMsg){ for(String ext : extMsg ){ builder.append("[").append(ext).append("]"); } } this.extMsg = builder.toString(); } /** * * @param code 错误码 * @param msg 描述信息 */ public ServiceException(String code, String msg) { super(code+":"+msg); this.code = code; this.msg = msg; } /** * 带Exception的构造方法,传format字符数组 * @param errorCode 错误码基类 * @param e 异常 * @param extMsg 扩展信息,给出具体的错误值信息等 */ public ServiceException(ErrorCode errorCode, Throwable e,String param[] , String ...extMsg ) { super(null==errorCode ? "" : errorCode.getCode(), e); init(errorCode, param, extMsg); } /** * * @param code 错误码 * @param msg 描述信息 * @param e 异常 */ /*public ServiceException(String code, String msg,Throwable e) { super(code+":"+msg, e); this.code = code; this.msg = msg; }*/ /** * * * 方法说明:异常错误码 * * @return */ public String getCode() { return code; } /** * * * 方法说明:异常描述信息 * * @return */ public String getMsg() { return msg; } public String getExtMsg() { return extMsg; } @Override public String getMessage() { return super.getMessage() + ","+extMsg; } public static void main(String[] args) { } }
相关文章推荐
- python中的@符号
- Python for Everybody-Using Python to Access Web DatExtracting Data With Regular Expressions
- xshell的快捷键(非常实用)
- IOS Quartz2D 通过UIColor生成图片
- Android异常:ServiceConnection 泄露
- Spring4.2源码解析-下载源码并导入eclipse
- cmd命令终止和启动进程
- centos MYSQL 主从同步配置
- 使用zsh的十大优点
- Python Sql数据库增删改查操作简单封装
- 高速Android开发系列通信篇之EventBus
- 设计模式——桥接模式
- 树莓派安装系统并设置中文界面
- yum源的配置(centos6.5)
- UITableView优化技巧
- Linux基本操作命令
- DAO基类
- 51nod-1119 1119 机器人走方格 V2(组合数学+乘法逆元+快速幂)
- SQL Server 删除重复记录,只保留一条记录
- Java throw与throws区别