Class ExceptionUtils


  • public class ExceptionUtils
    extends java.lang.Object
    Contains static methods useful for manipulating exceptions.
    • Constructor Summary

      Constructors 
      Constructor Description
      ExceptionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends java.lang.Throwable>
      T
      findCause​(java.lang.Throwable t, java.lang.Class<T> type)
      Locates a particular type of exception, working its way via the cause property of each exception in the exception stack.
      static <T extends java.lang.Throwable>
      T
      findCause​(java.lang.Throwable t, java.lang.Class<T> type, PropertyAccess access)
      Locates a particular type of exception, working its way down via any property that returns some type of Exception.
      static boolean isAnnotationInStackTrace​(java.lang.Throwable t, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
      Tells whether an exception annotated with a given annotation is found in the stack trace.
      static java.lang.String toMessage​(java.lang.Throwable exception)
      Extracts the message from an exception.
      • Methods inherited from class java.lang.Object

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

      • findCause

        public static <T extends java.lang.Throwable> T findCause​(java.lang.Throwable t,
                                                                  java.lang.Class<T> type)
        Locates a particular type of exception, working its way via the cause property of each exception in the exception stack.
        Parameters:
        t - the outermost exception
        type - the type of exception to search for
        Returns:
        the first exception of the given type, if found, or null
      • findCause

        public static <T extends java.lang.Throwable> T findCause​(java.lang.Throwable t,
                                                                  java.lang.Class<T> type,
                                                                  PropertyAccess access)
        Locates a particular type of exception, working its way down via any property that returns some type of Exception. This is more expensive, but more accurate, than findCause(Throwable, Class) as it works with older exceptions that do not properly implement the (relatively new) cause property.
        Parameters:
        t - the outermost exception
        type - the type of exception to search for
        access - used to access properties
        Returns:
        the first exception of the given type, if found, or null
      • toMessage

        public static java.lang.String toMessage​(java.lang.Throwable exception)
        Extracts the message from an exception. If the exception's message is null, returns the exceptions class name.
        Parameters:
        exception - to extract message from
        Returns:
        message or class name
        Since:
        5.4
      • isAnnotationInStackTrace

        public static boolean isAnnotationInStackTrace​(java.lang.Throwable t,
                                                       java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
        Tells whether an exception annotated with a given annotation is found in the stack trace.
        Returns:
        true or false
        Since:
        5.8.3