美文网首页
JAVA基础篇26:实际项目中对Exception的装饰&使用

JAVA基础篇26:实际项目中对Exception的装饰&使用

作者: 小胖学编程 | 来源:发表于2022-08-29 19:56 被阅读0次

    定义业务异常类:

    public class BusinessException extends RuntimeException {
    
        //继承的顶级父类实现了序列化接口,子类还是需要显示的声明SerialVersionUID。
        private static final long serialVersionUID = -3516896655779527315L;
    
        private int code;
    
        /**
         * 直白的异常(便于排查问题)
         */
        private String bluntlyMessage;
    
        public static BusinessException of(int code, String message) {
            return new BusinessException(code, message, null, null);
        }
    
        public static BusinessException of(int code, String message, String bluntlyMessage) {
            return new BusinessException(code, message, bluntlyMessage, null);
        }
    
        public static BusinessException of(int code, String message, Throwable cause) {
            return new BusinessException(code, message, null, cause);
        }
    
        public static BusinessException of(int code, String message, String bluntlyMessage, Throwable cause) {
            return new BusinessException(code, message, bluntlyMessage, cause);
        }
    
    
        public BusinessException(int code, String message, String bluntlyMessage, Throwable cause) {
            super(message, cause);
            this.code = code;
            this.bluntlyMessage = bluntlyMessage;
        }
    
        public int getCode() {
            return code;
        }
    
        public String getBluntlyMessage() {
            return bluntlyMessage;
        }
    }
    

    异常枚举类:

    public enum ErrorCode {
    
    
        PARAM_ERROR(10001, "参数错误"),
        DATA_INVALID(10002, "数据异常"),
        ;
    
    
        private int code;
        private String msg;
    
        ErrorCode(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        public int getCode() {
            return this.code;
        }
    
        public String getMsg() {
            return this.msg;
        }
    
    
        public BusinessException toException() {
            return BusinessException.of(this.code, this.msg);
        }
    
        public BusinessException toException(String message) {
            return BusinessException.of(this.code, message);
        }
    
    
        public BusinessException toException(String message, Throwable e) {
            return BusinessException.of(this.code, message, ExceptionUtil.getLogErrorMessage(e), e);
        }
    
        public BusinessException formatException(String format, String... args) {
            return BusinessException.of(this.code, String.format(format, args));
        }
    }
    

    工具类:

    public class ExceptionUtil {
        //主键/唯一键冲突的异常信息
        public static final String UNIQUE_ERROR = "Duplicate entry";
    
        //判断返回的Message是否含有 UNIQUE_ERROR主键冲突。
        public static String getErrorCode(Exception e) {
            String errorCode = "";
            if (e.getCause() != null && e.getCause().getMessage() != null) {
                errorCode = e.getCause().getMessage();
            }
            if (Strings.isNullOrEmpty(errorCode) && e.getMessage() != null) {
                errorCode = e.getMessage();
            }
            return errorCode;
        }
    
    
        /**
         * 获取到异常描述
         *
         * @param e 异常信息
         * @return "异常信息||异常类名||异常位置"
         */
        public static String getLogErrorMessage(Throwable e) {
            StackTraceElement[] stackTrace = e.getStackTrace();
            StackTraceElement stackTraceElement = null;
            if (stackTrace != null && stackTrace.length >= 1) {
                stackTraceElement = stackTrace[0];
            }
            return e.getMessage() + "||" + e.getClass().getSimpleName() + "||" + stackTraceElement;
        }
    
        /**
         * 判断抛出的异常是否包含某类异常
         *
         * @param throwable 抛出的异常
         * @param clazz     是否包含的异常类
         * @return true表示throwable中包含clazz的异常,false表示throwable中不包含clazz异常。
         */
        public static boolean containThrowable(Throwable throwable, Class<? extends Throwable> clazz) {
            boolean result = false;
            Throwable t = throwable;
            while (t != null) {
                if (clazz.isAssignableFrom(t.getClass())) {
                    result = true;
                    break;
                }
                t = t.getCause();
            }
            return result;
        }
    
    }
    

    使用方式:

    public class Test {
        public static void main(String[] args) {
            try {
                int i=1/0;
            } catch (Exception e) {
                throw ErrorCode.PARAM_ERROR.toException("系统错误", e);
            }
        }
    }
    

    相关文章

    JAVA基础篇(2)-异常
    [JAVA基础篇23]—JAVA异常工具类ExceptionUtils的使用

    相关文章

      网友评论

          本文标题:JAVA基础篇26:实际项目中对Exception的装饰&使用

          本文链接:https://www.haomeiwen.com/subject/ibfrnrtx.html