Java之业务异常类BusinessException封装实例
2016-06-03 21:46
393 查看
开心一笑
幼儿园搞活动,共有三个班,每个班出场时,要喊口号/坏笑小一班喊的口号是:“小一,小一,勇争第一。”/拳头
小二班口号是:“小二,小二,独一无二。”/强
等到小三班出场,喊出了令在场所有人都乐趴下的口号:“小三,小三,爸爸的心肝!”
全场昏倒!
提出问题
如何对项目的业务异常类进行封装处理???解决问题
首先是开发一个BusinessException业务异常类,继承BusinessException,用来统一处理业务出现的各种异常。具体细节可以看类中的注释// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.evada.inno.core.exception; import com.evada.inno.common.constants.IMessage; import com.evada.inno.core.util.I18nUtils; import java.text.MessageFormat; import org.apache.commons.lang3.StringUtils; /** 这里继承RuntimeException异常 **/ public class BusinessException extends RuntimeException { private static final long serialVersionUID = 2332608236621015980L; /** 错误码 **/ private IMessage errorCode; private String type = "B-"; private Object[] msgArgs; /** 用于存放后端返回的数据 **/ private Object data; public BusinessException(Throwable cause) { super(cause); } public BusinessException(String message) { super(message); } public BusinessException(String message, Throwable cause) { super(message, cause); } public BusinessException(IMessage errorCode) { this.errorCode = errorCode; } public BusinessException(IMessage errorCode, Object data) { this.data = data; this.errorCode = errorCode; } public BusinessException(IMessage errorCode, Throwable cause) { super(cause); this.errorCode = errorCode; } public BusinessException(IMessage errorCode, Object[] msgArgs) { this.errorCode = errorCode; this.msgArgs = msgArgs; } public BusinessException(IMessage errorCode, Object[] msgArgs, Throwable cause) { super(cause); this.errorCode = errorCode; this.msgArgs = msgArgs; } public Object[] getMsgArgs() { return this.msgArgs; } public void setMsgArgs(Object[] msgArgs) { this.msgArgs = msgArgs; } public String getMsg() { String msg = ""; if(this.errorCode == null) { msg = this.getMessage(); return msg; } else { try { //这里只要知道可以通过错误码获得相关错误信息 msg = I18nUtils.getMessage(this.errorCode, this.getMsgArgs()); } catch (Exception var3) { msg = MessageFormat.format("错误代码: {0}, 错误参数: {1}, 国际化消息读取失败!", new Object[]{Integer.valueOf(this.errorCode.getCode()), StringUtils.join(this.getMsgArgs(), "|")}); } return msg; } } public String getType() { return this.type; } public void setType(String type) { this.type = type; } public IMessage getErrorCode() { return this.errorCode; } public void setErrorCode(IMessage errorCode) { this.errorCode = errorCode; } public Object getData() { return this.data; } public void setData(Object data) { this.data = data; } }
IMessage类代码如下:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.evada.inno.common.constants; public interface IMessage { String getCategory(); int getCode(); }
ErrorCode类代码:
package com.evada.de.common.constants; import com.evada.inno.common.constants.IMessage; /** * DE应用的错误码 * 4位错误码定义: * 首位,应用标识(DE为3); * 二号位,应用模erprise:2 |块标识(common:0 | core:1 | ent file-manage:3 | project:4 | requirement-component:5 | work-flow:6 | architecture:7); * 三四位为错误码 * Created by KQY on 2015/12/24. */ public interface ErrorCode extends com.evada.inno.common.constants.ErrorCode { enum De implements IMessage { /** *数据存在冲突,是否覆盖更新! */ dataObjectConflict(3731), /** * 该系统已被IT估算所引用,不能删除! */ quotationDontDel(3429); private int code; private String category; De(int code) { this.code = code; this.category = this.getClass().getSimpleName(); } public int getCode() { return code; } public String getCategory() { return category; } } }
messages_zh_CN.properties文件
Common.invalidErrorCode=错误码无法识别! De.dataObjectConflict=数据存在冲突,是否覆盖更新! De.quotationDontDel={0}已被IT估算所引用,不能删除!
最后是如何在项目中使用:
/** * 创建规则指引 * * @param deGuidanceRuleDTO * @return */ @Override public DeGuidanceRuleDTO create(DeGuidanceRuleDTO deGuidanceRuleDTO) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { DeGuidanceRule guidanceRule = GuidanceRuleRepository.findByRuleName(deGuidanceRuleDTO.getRuleName()); if(guidanceRule!=null){ //重点在这里,只要出现业务异常,统一抛出业务异常类,传入相关的错误码即可 throw new BusinessException(ErrorCode.De.alreadyExists); } DeGuidanceRule deGuidanceRule = new DeGuidanceRule(); deGuidanceRuleDTO.setId(UUIDUtils.generate()); deGuidanceRuleDTO.setStatus(StatusEnum.ENABLE.toString()); deGuidanceRuleDTO.setType(ArchitectureStatusEnum.UN_START.toString()); //添加文件关联 addFileRelations(deGuidanceRuleDTO.getId(), deGuidanceRuleDTO.getFiles()); PropertyUtils.copyProperties(deGuidanceRule, deGuidanceRuleDTO); GuidanceRuleRepository.saveAndFlush(deGuidanceRule); return GuidanceRuleDAO.findDtoById(deGuidanceRule.getId()); }
读书感悟
不管你曾经被伤害得有多深,总会有一个人的出现,让你原谅之前生活对你所有的刁难。到不了的地方都叫做远方,回不去的世界都叫做家乡,我一直向往的却是比远更远的地方。
你说你会爱我一辈子,我真傻,居然忘了问“是这辈子还是下辈子” 。
其他
如果有带给你一丝丝小快乐,就让快乐继续传递下去,欢迎转载,点赞,顶,欢迎留下宝贵的意见,多谢支持!相关文章推荐
- java 实现接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写
- java web1
- Java编程思想第四版第十一章学习——持有对象
- 《Java编程思想》学习笔记(第一章):Java世界里的“对象”(上)
- Java并发包解析01
- spring aop记录操作日志和错误日志
- Java并发包概览
- 使用ZooKeeper实现Java跨JVM的分布式锁(读写锁)
- Spring基础学习(四)—AOP
- java并发编程实战 java并发编程的艺术 阅读随笔
- SpringMVC+Spring4.0+Hibernate 简单的整合
- java 反射机制
- Java集合框架图解
- eclipse 连接 mysql
- java练习——用循环输出等腰三角形
- 最长公共子序列&&最长公共子串---[动态规划]
- java spring
- eclipse中手动导入DTD文件的方式
- Eclipse安卓奇怪编译问题解决记事
- java清除地图道路信息