自定义异常

简介: 自定义异常
@Data@EqualsAndHashCode(callSuper=true)
publicclassOpenRuntimeExceptionextendsRuntimeException {
protectedStringerrorCode="Internal Server Error";
protectedStringmessage="No value present";
protectedintstatus=500;
publicOpenRuntimeException(OpenErrorCodessoErrorCode) {
this(ssoErrorCode, (Object[]) null);
    }
OpenRuntimeException(OpenErrorCodessoErrorCode, Object... params) {
this(ssoErrorCode, null, params);
    }
OpenRuntimeException(OpenErrorCodessoErrorCode, Throwablecause, Object... params) {
this.errorCode=ssoErrorCode.name();
this.status=ssoErrorCode.getStatus();
this.message=ssoErrorCode.getMessage(params);
if (cause!=null) {
initCause(cause);
        }
    }
@OverridepublicStringgetMessage() {
returnmessage;
    }
}

publicinterfaceOpenErrorCode {
/*** description: 获取异常的name** @return {@link String}* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/4/25 9:11*/Stringname();
/*** description: 响应的状态码** @return {@link int}* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/4/25 9:12*/intgetStatus();
/*** description:** @param cause  异常对象* @param params 格式化参数* @return {@link OpenRuntimeException}* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/4/25 9:58*/defaultOpenRuntimeExceptionruntimeException(Throwablecause, Object... params) {
returnnewOpenRuntimeException(this, cause, params);
    }
/*** description:** @param params 格式化参数* @return {@link OpenRuntimeException}* @throws* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/4/25 10:00*/defaultOpenRuntimeExceptionruntimeException(Object... params) {
returnnewOpenRuntimeException(this, params);
    }
/*** description:** @param cause 异常* @return {@link OpenRuntimeException}* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/4/25 16:40*/defaultOpenRuntimeExceptionruntimeException(Throwablecause) {
returnnewOpenRuntimeException(this, cause);
    }
/*** description:** @param params 格式化参数* @param locale 地区* @return {@link String}* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/4/25 9:13*/defaultStringgetMessage(Localelocale, Object... params) {
Stringmessage="NO MESSAGE!!!";
if (locale==null) {
locale=Locale.getDefault();
        }
try {
Stringformatter=Objects.equals("OpenExceptionEnum", this.getClass().getSimpleName()) ?"META-INF.{0}" : "i18n.exception.{0}";
ResourceBundlebundle=ResourceBundle                    .getBundle(MessageFormat.format(formatter,
this.getClass().getSimpleName()), locale);
message=bundle.getString(this.name());
returnnewMessageFormat(message).format(params);
        } catch (Throwablee) {
if (!(einstanceofMissingResourceException)) {
e.printStackTrace();
            }
message=Objects.isNull(params) ?message : newMessageFormat("{0}").format(params);
returnmessage;
        }
    }
/*** description:** @param params 格式化参数* @return {@link String}* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/4/25 9:56*/defaultStringgetMessage(Object... params) {
returngetMessage(Locale.getDefault(), params);
    }
}
相关文章
|
10月前
|
C++
65 C++ - 自定义异常
65 C++ - 自定义异常
38 0
|
2月前
|
前端开发 Java Spring
项目使用 GlobalExceptionHandler 与 @RestControllerAdvice自定义异常 二
项目使用 GlobalExceptionHandler 与 @RestControllerAdvice自定义异常 二
29 3
|
2月前
|
Java 数据格式 Spring
项目使用 GlobalExceptionHandler 自定义异常 一
项目使用 GlobalExceptionHandler 自定义异常 一
52 3
|
4月前
|
Java
自定义异常类
自定义异常类
17 0
|
Java
如何自定义异常类
如何自定义异常类
43 0
|
4月前
|
Java
java异常处理,如何自定义异常?
java异常处理,如何自定义异常?
86 4
|
11月前
|
Java
异常的处理和自定义异常
在Java中,异常(Exception)是指程序在运行过程中遇到的错误或异常情况。Java提供了异常处理机制,可以捕获和处理异常,使程序在遇到异常时能够继续执行或进行相应的处理。 异常处理的基本结构是try-catch语句块。在try块中编写可能抛出异常的代码,如果在try块中抛出了异常,那么程序会跳转到catch块中执行相应的异常处理代码。 以下是一个简单的异常处理的示例: ```java try { // 可能抛出异常的代码 int result = 10 / 0; } catch (ArithmeticException e) { // 异常处理代码
43 0
|
SQL 数据安全/隐私保护
异常处理与自定义异常
异常处理与自定义异常
52 0
|
程序员
3、自定义异常处理
查询条件: 站点id、模板id、页面别名
81 0
|
数据库 数据安全/隐私保护 开发者
自定义异常|学习笔记
快速学习自定义异常