Enum ExceptionUtil

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<ExceptionUtil>

    public enum ExceptionUtil
    extends java.lang.Enum<ExceptionUtil>
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      SINGLETON  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.StringBuilder appendStackTrace​(java.lang.StringBuilder builder, java.lang.Throwable throwable)  
      static java.lang.StringBuilder appendStackTraceElement​(java.lang.StringBuilder builder, java.lang.StackTraceElement element)
      Appends a StackTraceElement in the same way that StackTraceElement.toString() would except without creating intermediate String objects.
      static <E extends java.lang.Throwable>
      E
      cause​(java.lang.Throwable throwable, java.lang.Class<E> clazz)
      Return the specific cause of the given Throwable if the cause is one of the classes provided; otherwise return null.
      static <T> T checkException​(java.util.concurrent.Callable<T> callable, java.lang.String exceptionMessage)
      Utility method to catch checked exceptions from a Callable and to rethrow as a RuntimeException.
      static java.lang.Throwable getCause​(java.lang.Throwable throwable, java.lang.Class<?>... classes)
      Return the cause of the given Throwable if the cause is one of the classes provided; otherwise return the Throwable itself.
      static java.lang.String getStackTrace​(java.lang.Throwable throwable)  
      static <E extends java.lang.Throwable>
      E
      unwrap​(java.lang.Throwable throwable, java.lang.Class<? extends E> clazz)
      Unwrap the cause of the given Throwable if the cause is assignable to the provided class; otherwise return null.
      static java.lang.Throwable unwrapCompletion​(java.lang.Throwable throwable)
      Unwrap the cause of the given Throwable if the cause is assignable to the provided class; otherwise return null.
      static ExceptionUtil valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static ExceptionUtil[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      static <T extends java.lang.Throwable>
      T
      withoutStackTrace​(T throwable)  
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

    • Method Detail

      • values

        public static ExceptionUtil[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (ExceptionUtil c : ExceptionUtil.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static ExceptionUtil valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • getCause

        public static java.lang.Throwable getCause​(java.lang.Throwable throwable,
                                                   @Nonnull
                                                   java.lang.Class<?>... classes)
        Return the cause of the given Throwable if the cause is one of the classes provided; otherwise return the Throwable itself. If there are multiple matches, this method will return the cause deepest in the getCause() stack (e.g. closest to the root cause).

        For example, consider "ex", a RuntimeException caused by SQLException caused by ConnectException.

        • getCause(ex, ConnectException.class) --> returns the ConnectException
        • getCause(ex, SomeOtherException.class, ConnectException.class) --> returns the ConnectException
        • getCause(ex, SQLException.class, ConnectException.class) --> returns the ConnectException
        • getCause(ex, ConnectException.class, SQLException.class) --> returns the ConnectException
        • getCause(ex, SomeOtherException.class) --> returns ex

        Note that subclasses do not match. For example, looking for RuntimeException would not match to an IndexOutOfBoundsException. This is intentional.

        Parameters:
        throwable - Throwable to find the cause of
        classes - classes to look for in the cause stack
        Returns:
        cause of exception.
      • cause

        public static <E extends java.lang.Throwable> E cause​(java.lang.Throwable throwable,
                                                              @Nonnull
                                                              java.lang.Class<E> clazz)
        Return the specific cause of the given Throwable if the cause is one of the classes provided; otherwise return null. If there are multiple matches, this method will return the cause deepest in the getCause() stack (e.g. closest to the root cause).

        For example, consider "ex", a RuntimeException caused by SQLException caused by ConnectException.

        • cause(ex, ConnectException.class) --> returns the ConnectException
        • cause(ex, SomeOtherException.class) --> returns null

        Note that subclasses do not match. For example, looking for RuntimeException would not match to an IndexOutOfBoundsException. This is intentional.

        Parameters:
        throwable - Throwable to find the cause of
        clazz - classes to look for in the cause stack
        Returns:
        cause of exception.
      • unwrap

        public static <E extends java.lang.Throwable> E unwrap​(java.lang.Throwable throwable,
                                                               @Nonnull
                                                               java.lang.Class<? extends E> clazz)
        Unwrap the cause of the given Throwable if the cause is assignable to the provided class; otherwise return null. If there are multiple matches, this method will return the cause deepest in the getCause() stack (e.g. closest to the root cause).

        For example, consider "ex", a RuntimeException caused by SQLException caused by ConnectException.

        • getCause(ex, ConnectException.class) --> returns the ConnectException
        • getCause(ex, SomeOtherException.class) --> returns null

        Parameters:
        throwable - Throwable to find the cause of
        clazz - classes to look for in the cause stack
        Returns:
        cause of exception.
      • unwrapCompletion

        @Nonnull
        public static java.lang.Throwable unwrapCompletion​(@Nonnull
                                                           java.lang.Throwable throwable)
        Unwrap the cause of the given Throwable if the cause is assignable to the provided class; otherwise return null. If there are multiple matches, this method will return the cause deepest in the getCause() stack (e.g. closest to the root cause).

        For example, consider "ex", a RuntimeException caused by SQLException caused by ConnectException.

        • getCause(ex) --> returns the ConnectException
        For example, consider "ex", a NullPointerException.
        • getCause(ex) --> returns the NullPointerException

        Parameters:
        throwable - Throwable to find the cause of
        Returns:
        cause of exception.
      • getStackTrace

        public static java.lang.String getStackTrace​(@Nonnull
                                                     java.lang.Throwable throwable)
      • appendStackTrace

        @Nonnull
        public static java.lang.StringBuilder appendStackTrace​(@Nonnull
                                                               java.lang.StringBuilder builder,
                                                               @Nonnull
                                                               java.lang.Throwable throwable)
      • appendStackTraceElement

        public static java.lang.StringBuilder appendStackTraceElement​(java.lang.StringBuilder builder,
                                                                      java.lang.StackTraceElement element)
        Appends a StackTraceElement in the same way that StackTraceElement.toString() would except without creating intermediate String objects.
        Parameters:
        builder - StringBuilder
        element - StackTraceElement
        Returns:
        StringBuilder
      • withoutStackTrace

        public static <T extends java.lang.Throwable> T withoutStackTrace​(@Nonnull
                                                                          T throwable)
      • checkException

        public static <T> T checkException​(@Nonnull
                                           java.util.concurrent.Callable<T> callable,
                                           @Nonnull
                                           java.lang.String exceptionMessage)
        Utility method to catch checked exceptions from a Callable and to rethrow as a RuntimeException.
        Parameters:
        callable - Callable with checked exception
        exceptionMessage - Message for thrown exception
        Returns:
        return value of callable