Java之業務異常類BusinessException封裝例項
阿新 • • 發佈:2019-02-15
開心一笑
幼兒園搞活動,共有三個班,每個班出場時,要喊口號/壞笑
小一班喊的口號是:“小一,小一,勇爭第一。”/拳頭
小二班口號是:“小二,小二,獨一無二。”/強
等到小三班出場,喊出了令在場所有人都樂趴下的口號:“小三,小三,爸爸的心肝!”
全場昏倒!
視訊教程
大家好,我錄製的視訊《Java之優雅程式設計之道》已經在CSDN學院釋出了,有興趣的同學可以購買觀看,相信大家一定會收穫到很多知識的。謝謝大家的支援……
提出問題
如何對專案的業務異常類進行封裝處理???
解決問題
首先是開發一個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());
}
讀書感悟
- 不管你曾經被傷害得有多深,總會有一個人的出現,讓你原諒之前生活對你所有的刁難。
- 到不了的地方都叫做遠方,回不去的世界都叫做家鄉,我一直嚮往的卻是比遠更遠的地方。
- 你說你會愛我一輩子,我真傻,居然忘了問“是這輩子還是下輩子” 。
其他
如果有帶給你一絲絲小快樂,就讓快樂繼續傳遞下去,歡迎轉載,點贊,頂,歡迎留下寶貴的意見,多謝支援!