您的位置:首页 > 编程语言 > Java开发

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());
}


读书感悟

不管你曾经被伤害得有多深,总会有一个人的出现,让你原谅之前生活对你所有的刁难。

到不了的地方都叫做远方,回不去的世界都叫做家乡,我一直向往的却是比远更远的地方。

你说你会爱我一辈子,我真傻,居然忘了问“是这辈子还是下辈子” 。

其他

如果有带给你一丝丝小快乐,就让快乐继续传递下去,欢迎转载,点赞,顶,欢迎留下宝贵的意见,多谢支持!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: