类 ExceptionUtils

java.lang.Object
cn.taketoday.util.ExceptionUtils

public abstract class ExceptionUtils extends Object
从以下版本开始:
2018-11-13 21:25
作者:
Harry Yang
  • 构造器详细资料

    • ExceptionUtils

      public ExceptionUtils()
  • 方法详细资料

    • unwrapThrowable

      public static Throwable unwrapThrowable(Throwable ex)
      Unwrap
      参数:
      ex - target Throwable
      返回:
      unwrapped Throwable
    • getNestedMessage

      @Nullable public static String getNestedMessage(@Nullable Throwable cause, @Nullable String message)
      Build a message for the given base message and root cause.
      参数:
      message - the base message
      cause - the root cause
      返回:
      the full exception message
      从以下版本开始:
      3.0
    • getRootCause

      @Nullable public static Throwable getRootCause(@Nullable Throwable original)
      Retrieve the innermost cause of the given exception, if any.
      参数:
      original - the original exception to introspect
      返回:
      the innermost exception, or null if none
      从以下版本开始:
      3.0
    • getMostSpecificCause

      public static Throwable getMostSpecificCause(Throwable original)
      Retrieve the most specific cause of the given exception, that is, either the innermost cause (root cause) or the exception itself.

      Differs from getRootCause(java.lang.Throwable) in that it falls back to the original exception if there is no root cause.

      参数:
      original - the original exception to introspect
      返回:
      the most specific cause (never null)
      从以下版本开始:
      3.0
    • contains

      public static boolean contains(Throwable ex, @Nullable Class<?> exType)
      Check whether this exception contains an exception of the given type: either it is of the given class itself or it contains a nested cause of the given type.
      参数:
      exType - the exception type to look for
      返回:
      whether there is a nested exception of the specified type
    • sneakyThrow

      public static RuntimeException sneakyThrow(@Nullable Throwable t)
      Throws any throwable 'sneakily' - you don't need to catch it, nor declare that you throw it onwards. The exception is still thrown - javac will just stop whining about it.

      Example usage:

         public void run() {
           throw sneakyThrow(new IOException("You don't need to catch me!"));
         }
       

      NB: The exception is not wrapped, ignored, swallowed, or redefined. The JVM actually does not know or care about the concept of a 'checked exception'. All this method does is hide the act of throwing a checked exception from the java compiler.

      Note that this method has a return type of RuntimeException; it is advised you always call this method as argument to the throw statement to avoid compiler errors regarding no return statement and similar problems. This method won't of course return an actual RuntimeException - it never returns, it always throws the provided exception.

      参数:
      t - The throwable to throw without requiring you to catch its type.
      返回:
      A dummy RuntimeException; this method never returns normally, it always throws an exception!
      从以下版本开始:
      4.0
    • sneakyThrow0

      private static <T extends Throwable> T sneakyThrow0(Throwable t) throws T
      抛出:
      T extends Throwable
    • sneakyThrow

      public static void sneakyThrow(ExceptionUtils.Action action)
      从以下版本开始:
      4.0
    • sneakyThrow

      public static <T> T sneakyThrow(Callable<T> action)
      从以下版本开始:
      4.0
    • stackTraceToString

      public static String stackTraceToString(Throwable cause)
      Gets the stack trace from a Throwable as a String.
      参数:
      cause - the Throwable to be examined
      返回:
      the stack trace as generated by Throwable.printStackTrace(java.io.PrintWriter) method.