Class ExceptionHelper


  • public final class ExceptionHelper
    extends java.lang.Object
    Helper methods dealing with exceptions.
    Author:
    harald
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ExceptionHelper.Handler<T extends java.lang.Throwable>
      Exception handler.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String concatenateMessages​(java.lang.Throwable headOfChain)
      Concatenates the messages of an exception chain.
      static java.lang.Throwable extractException​(boolean first, java.lang.Throwable headOfChain, java.lang.Class<? extends java.lang.Throwable>... types)
      Extracts any of the given exception types from an exception chain.
      static <T extends java.lang.Throwable>
      T
      extractException​(java.lang.Class<T> type, boolean first, java.lang.Throwable headOfChain)
      Extracts the given exception type from an exception chain.
      static java.lang.StackTraceElement[] filterStackTrace​(java.lang.StackTraceElement[] trace)
      Filters a given stacktrace skipping all noisy elements without relevant information for the developer.
      static java.lang.String getStackTraceAsString​(java.lang.Throwable cause)
      Returns the stacktrace of a given exception as a string.
      static boolean handleException​(boolean first, java.lang.Throwable headOfChain, ExceptionHelper.Handler<? extends java.lang.Throwable>... handlers)
      Extracts any of the given exception types from an exception chain.
      static boolean isClassValuableForStackTrace​(java.lang.String className)
      Checks if the class is a valuable information within a stracktrace.
      • Methods inherited from class java.lang.Object

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

      • extractException

        public static <T extends java.lang.Throwable> T extractException​(java.lang.Class<T> type,
                                                                         boolean first,
                                                                         java.lang.Throwable headOfChain)
        Extracts the given exception type from an exception chain.
        Type Parameters:
        T - the exception type to search for
        Parameters:
        type - the exception class to search for
        first - true if finds the first, else the last in chain
        headOfChain - the head of chained exceptions, may be null
        Returns:
        the exception, null if none
      • extractException

        public static java.lang.Throwable extractException​(boolean first,
                                                           java.lang.Throwable headOfChain,
                                                           java.lang.Class<? extends java.lang.Throwable>... types)
        Extracts any of the given exception types from an exception chain.
        Parameters:
        first - true if finds the first, else the last in chain
        headOfChain - the head of chained exceptions, may be null
        types - the exception classes to search for
        Returns:
        the exception, null if none
      • handleException

        public static boolean handleException​(boolean first,
                                              java.lang.Throwable headOfChain,
                                              ExceptionHelper.Handler<? extends java.lang.Throwable>... handlers)
        Extracts any of the given exception types from an exception chain.
        Parameters:
        first - true if handle the first, else the last in chain
        headOfChain - the head of chained exceptions, may be null
        handlers - the exception handlers
        Returns:
        true if handled, false if no matching exception found in chain
      • concatenateMessages

        public static java.lang.String concatenateMessages​(java.lang.Throwable headOfChain)
        Concatenates the messages of an exception chain.
        Parameters:
        headOfChain - the head of chained exceptions, may be null
        Returns:
        the message string
      • getStackTraceAsString

        public static java.lang.String getStackTraceAsString​(java.lang.Throwable cause)
        Returns the stacktrace of a given exception as a string.
        Parameters:
        cause - the exception
        Returns:
        the stacktrace as a printable string
      • isClassValuableForStackTrace

        public static boolean isClassValuableForStackTrace​(java.lang.String className)
        Checks if the class is a valuable information within a stracktrace.
        Parameters:
        className - the classname
        Returns:
        true if valuable, else noise
      • filterStackTrace

        public static java.lang.StackTraceElement[] filterStackTrace​(java.lang.StackTraceElement[] trace)
        Filters a given stacktrace skipping all noisy elements without relevant information for the developer.
        Parameters:
        trace - the stacktrace
        Returns:
        the reduced stacktrace