public class CustomException extends RuntimeException { private static final long serialVersionUID = 1L; private Integer code; private String message; private Object data; private String throwTime; public CustomException(ErrorCodeEnum errorCodeEnum) { super(errorCodeEnum.msg()); this.code = errorCodeEnum.code(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); this.throwTime = dateFormat.format(date); } public CustomException(String message) { this.message = message; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); this.throwTime = dateFormat.format(date); } public CustomException(String message, Integer code) { this.message = message; this.code = code; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); this.throwTime = dateFormat.format(date); } public CustomException(String message, Integer code, Object data) { this.message = message; this.code = code; this.data = data; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); this.throwTime = dateFormat.format(date); } public CustomException(String message,Object data) { this.message = message; this.data = data; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); this.throwTime = dateFormat.format(date); } public CustomException(String message, Throwable e) { super(message, e); this.message = message; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); this.throwTime = dateFormat.format(date); } @Override public String getMessage() { return message; } public Integer getCode() { return code; } public Object getData() { return data; } @Override public String toString() { return "CustomException{" + "throwTime=" + throwTime + ", code=" + code + ", message='" + message + '\'' + ", data=" + data + '}'; } }
给大家分享一个自己常用的自定义异常类
最新推荐文章于 2024-08-03 21:02:21 发布