自定义异常类
package net.xdclass.order_service.Exception;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.ws.soap.SOAPFaultException;
public class CloudException extends RuntimeException {
private static final long serialVersionUID = 1L;
private ExceptionDefault exceptionDefault;
public ExceptionDefault getExceptionDefault() {
return exceptionDefault;
}
public void setExceptionDefault(ExceptionDefault exceptionDefault) {
this.exceptionDefault = exceptionDefault;
}
public CloudException(ExceptionDefault exceptionDefault) {
super();
this.exceptionDefault = exceptionDefault;
}
public CloudException(String message, ExceptionDefault exceptionDefault) {
super(message);
this.exceptionDefault = exceptionDefault;
}
@Override
public Throwable getCause() {
SOAPFault fault;
try {
fault = SOAPFactory.newInstance().createFault();
fault.setFaultString(this.getExceptionDefault().getMessage());
SOAPFaultException ex = new SOAPFaultException(fault);
return ex;
} catch (SOAPException e) {
e.printStackTrace();
return super.getCause();
}
}
}
异常格式
package net.xdclass.order_service.Exception;
public class ExceptionDefault {
private String code; //编码
private String message; //描述
public ExceptionDefault() {
super();
}
public ExceptionDefault(String code, String message) {
super();
this.code = code;
this.message = message;
}
@Override
public String toString() {
StringBuffer sf = new StringBuffer();
sf.append("异常编码: ").append(this.code).append(",");
sf.append("异常具体信息: ").append(this.message);
return sf.toString();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
异常类型,通过枚举类来列举,业务类型异常
package net.xdclass.order_service.Exception;
import java.util.HashMap;
import java.util.Map;
public enum EnumExceptionCode {
E000000("000000", "成功"),
E000001("000001", "查询成功无数据"),
E000002("000002", "调用成功,异步处理中....");
public static EnumExceptionCode toE_ExceptionCode(String code) {
return codeMap.get(code);
}
public static String getNameByKey(String code) {
EnumExceptionCode procResult = codeMap.get(code);
if (procResult != null) {
return procResult.getName();
}
return "";
}
private static Map<String, EnumExceptionCode> codeMap;
private static Map<String, EnumExceptionCode> nameMap;
private EnumExceptionCode(String code, String name) {
this.code = code;
this.name = name;
}
static {
codeMap = new HashMap<String, EnumExceptionCode>();
nameMap = new HashMap<String, EnumExceptionCode>();
for (EnumExceptionCode procResult : EnumExceptionCode.values()) {
codeMap.put(procResult.getCode(), procResult);
nameMap.put(procResult.getName(), procResult);
}
}
private String code;
private String name;
public boolean equals(String code) {
return this.getCode().equals(code);
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public String toString() {
return this.getCode();
}
}
抛出异常工具类
package net.xdclass.order_service.utils;
import net.xdclass.order_service.Exception.CloudException;
import net.xdclass.order_service.Exception.EnumExceptionCode;
import net.xdclass.order_service.Exception.ExceptionDefault;
public class ExceptionUtil {
/**
* 抛出异常信息
*
* @param key
* @param exceptionCode
* @throws CloudException
*/
public static void throwCloudException(String key, String exceptionCode) throws CloudException {
ExceptionDefault fault = new ExceptionDefault();
fault.setCode(exceptionCode);
fault.setMessage(EnumExceptionCode.getNameByKey(exceptionCode)+":"+key);
throw new CloudException(EnumExceptionCode.getNameByKey(exceptionCode)+":"+key , fault);
}
}
抛出异常信息
if(null != costRecord.getDailyBillCode()){
ExceptionUtil.throwException(EnumExceptionCode.E000001.getCode(),"");
}
获取异常信息
try{
}catch (CreditServiceException e){
e.getExceptionDefault().getCode();//enum的code
EnumExceptionCode.getNameByKey(e.getExceptionDefault().getCode());//enum描述
e.getExceptionDefault().getMessage();//错误描述
}