Class ExceptionUtil


  • public class ExceptionUtil
    extends java.lang.Object
    Few exception utilities.
    • Constructor Summary

      Constructors 
      Constructor Description
      ExceptionUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String buildMessage​(java.lang.String message, java.lang.Throwable cause)
      Build a message for the given base message and its cause.
      static java.lang.String exceptionChainToString​(java.lang.Throwable t)
      Prints full exception stack trace, from top to root cause, into a String.
      static java.lang.String exceptionStackTraceToString​(java.lang.Throwable t)
      Prints stack trace into a String.
      static <T extends java.lang.Throwable>
      T
      findCause​(java.lang.Throwable throwable, java.lang.Class<T> cause)
      Finds throwing cause in exception stack.
      static java.lang.StackTraceElement[] getCurrentStackTrace()
      Returns current stack trace in form of array of stack trace elements.
      static java.lang.Throwable[] getExceptionChain​(java.lang.Throwable throwable)
      Returns exception chain starting from top up to root cause.
      static java.lang.Throwable getRootCause​(java.lang.Throwable throwable)
      Introspects the Throwable to obtain the root cause.
      static java.lang.StackTraceElement[] getStackTrace​(java.lang.Throwable t, java.lang.String[] allow, java.lang.String[] deny)
      Returns stack trace filtered by class names.
      static java.lang.StackTraceElement[][] getStackTraceChain​(java.lang.Throwable t, java.lang.String[] allow, java.lang.String[] deny)
      Returns stack trace chain filtered by class names.
      static java.lang.String message​(java.lang.Throwable throwable)
      Returns non-null message for a throwable.
      static java.sql.SQLException rollupSqlExceptions​(java.util.Collection<java.sql.SQLException> exceptions)
      Rolls up SQL exceptions by taking each proceeding exception and making it a child of the previous using the setNextException method of SQLException.
      static void throwRuntimeException​(java.lang.Throwable throwable)
      Throws checked exceptions in un-checked manner.
      static java.lang.Throwable unwrapThrowable​(java.lang.Throwable wrappedThrowable)
      Unwraps invocation and undeclared exceptions to real cause.
      static java.lang.Exception wrapToException​(java.lang.Throwable throwable)  
      static java.lang.RuntimeException wrapToRuntimeException​(java.lang.Throwable throwable)
      Wraps exception to RuntimeException.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExceptionUtil

        public ExceptionUtil()
    • Method Detail

      • getCurrentStackTrace

        public static java.lang.StackTraceElement[] getCurrentStackTrace()
        Returns current stack trace in form of array of stack trace elements. First stack trace element is removed. Since an exception is thrown internally, this method is slow.
      • getStackTrace

        public static java.lang.StackTraceElement[] getStackTrace​(java.lang.Throwable t,
                                                                  java.lang.String[] allow,
                                                                  java.lang.String[] deny)
        Returns stack trace filtered by class names.
      • getStackTraceChain

        public static java.lang.StackTraceElement[][] getStackTraceChain​(java.lang.Throwable t,
                                                                         java.lang.String[] allow,
                                                                         java.lang.String[] deny)
        Returns stack trace chain filtered by class names.
      • getExceptionChain

        public static java.lang.Throwable[] getExceptionChain​(java.lang.Throwable throwable)
        Returns exception chain starting from top up to root cause.
      • exceptionStackTraceToString

        public static java.lang.String exceptionStackTraceToString​(java.lang.Throwable t)
        Prints stack trace into a String.
      • exceptionChainToString

        public static java.lang.String exceptionChainToString​(java.lang.Throwable t)
        Prints full exception stack trace, from top to root cause, into a String.
      • buildMessage

        public static java.lang.String buildMessage​(java.lang.String message,
                                                    java.lang.Throwable cause)
        Build a message for the given base message and its cause.
      • getRootCause

        public static java.lang.Throwable getRootCause​(java.lang.Throwable throwable)
        Introspects the Throwable to obtain the root cause.

        This method walks through the exception chain to the last element, "root" of the tree, and returns that exception. If no root cause found returns provided throwable.

      • findCause

        public static <T extends java.lang.Throwable> T findCause​(java.lang.Throwable throwable,
                                                                  java.lang.Class<T> cause)
        Finds throwing cause in exception stack. Returns throwable object if cause class is matched. Otherwise, returns null.
      • rollupSqlExceptions

        public static java.sql.SQLException rollupSqlExceptions​(java.util.Collection<java.sql.SQLException> exceptions)
        Rolls up SQL exceptions by taking each proceeding exception and making it a child of the previous using the setNextException method of SQLException.
      • throwRuntimeException

        public static void throwRuntimeException​(java.lang.Throwable throwable)
        Throws checked exceptions in un-checked manner.
      • message

        public static java.lang.String message​(java.lang.Throwable throwable)
        Returns non-null message for a throwable.
      • wrapToRuntimeException

        public static java.lang.RuntimeException wrapToRuntimeException​(java.lang.Throwable throwable)
        Wraps exception to RuntimeException.
      • wrapToException

        public static java.lang.Exception wrapToException​(java.lang.Throwable throwable)
      • unwrapThrowable

        public static java.lang.Throwable unwrapThrowable​(java.lang.Throwable wrappedThrowable)
        Unwraps invocation and undeclared exceptions to real cause.