# DefaultRuntimeException

默认的运行时异常

### 支持 `{}` 占位符,

示例:

```java
throw new DefaultRuntimeException("dataName:[{}],keys:[{}] can't be null/empty!", dataName, beans.keySet());
```

此外主要作用,是在异常message 中追加 cause exception信息,方便查看排查问题

### 示例1:

如下代码,使用原生RuntimeException

```java
  
  public void testRuntimeException(){
      try{
          int i = 1 / 0;
      }catch (Exception e){
          throw new RuntimeException("", e);
      }
  }
```

抛出的异常情况在控制台是这样的:

```java

java.lang.RuntimeException: 
    at com.feilong.core.DefaultRuntimeExceptionTest.testRuntimeException(DefaultRuntimeExceptionTest.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:606)
Caused by: java.lang.ArithmeticException: / by zero
    at com.feilong.core.DefaultRuntimeExceptionTest.testRuntimeException(DefaultRuntimeExceptionTest.java:61)
    ... 23 more
```

而如果使用 `DefaultRuntimeException`

```java
  
  public void testDefaultRuntimeException(){
      try{
          int i = 1 / 0;
      }catch (Exception e){
          throw new DefaultRuntimeException("", e);
      }
  }
  
```

抛出来的信息是这样的 :

```java
com.feilong.core.DefaultRuntimeException: <span style="color:red">java.lang.ArithmeticException: / by zero</span>
    at com.feilong.core.DefaultRuntimeExceptionTest.testDefaultRuntimeException(DefaultRuntimeExceptionTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

Caused by: java.lang.ArithmeticException: / by zero
    at com.feilong.core.DefaultRuntimeExceptionTest.testDefaultRuntimeException(DefaultRuntimeExceptionTest.java:51)
    ... 23 more
```

### 示例2:

如下代码

```java
  
  public void testRuntimeExceptionMessage(){
      try{
          int i = 1 / 0;
      }catch (Exception e){
          throw new RuntimeException("exception", e);
      }
  }
  
```

抛出的异常情况在控制台是这样的:

```java
java.lang.RuntimeException: exception
    at com.feilong.core.DefaultRuntimeExceptionTest.testRuntimeExceptionMessage(DefaultRuntimeExceptionTest.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ArithmeticException: / by zero
    at com.feilong.core.DefaultRuntimeExceptionTest.testRuntimeExceptionMessage(DefaultRuntimeExceptionTest.java:103)
    ... 23 more
```

而如果使用 DefaultRuntimeException

```java
  public void testDefaultRuntimeExceptionMessageAppend(){
      try{
          int i = 1 / 0;
      }catch (Exception e){
          throw new DefaultRuntimeException("exception", e);
      }
  }
```

抛出来的信息是这样的 :

```java
com.feilong.core.DefaultRuntimeException: <span style="color:red">exception,cause by:[java.lang.ArithmeticException: / by zero]</span>
    at com.feilong.core.DefaultRuntimeExceptionTest.testDefaultRuntimeExceptionMessageAppend(DefaultRuntimeExceptionTest.java:95)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ArithmeticException: / by zero
    at com.feilong.core.DefaultRuntimeExceptionTest.testDefaultRuntimeExceptionMessageAppend(DefaultRuntimeExceptionTest.java:93)
    ... 23 more
  </pre>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://feilong.gitbook.io/feilong-docs/com.feilong.core/defaultruntimeexception.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
