Class Exceptions


  • public class Exceptions
    extends java.lang.Object

    Utility class to handle Throwable objects and stack traces. This implementation is compatible with all Java and Android versions.

    Since:
    0.1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      Exceptions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearGroupPackages()
      Clears previously registered group packages.
      static void clearIgnorePackages()
      Clears previously registered ignore packages.
      static void clearRootPackages()
      Clears previously registered root packages.
      static boolean containsCause​(java.lang.Throwable throwable, java.lang.Class<?> causeClass)
      Returns true if the given cause class is found in the exception chain of the throwable.
      static boolean containsCause​(java.lang.Throwable throwable, java.lang.Class<?> causeClass, java.lang.String causeMessage)
      Returns true if the given cause class and cause message is found in the exception chain of the throwable.
      static java.lang.String getAllMessages​(java.lang.Throwable throwable)
      Returns all messages found in the exception chain of the throwable as a single string.
      static java.lang.Throwable getCause​(java.lang.Throwable throwable)
      Returns the cause of the throwable by walking through the exception chain.
      static java.lang.Throwable getCause​(java.lang.Throwable throwable, int maxDepth)
      Returns the cause of the throwable by walking through the exception chain.
      static boolean getIgnoreAllCauses()
      Returns the value of ignore all causes option.
      static com.arthenica.smartexception.StackTraceElementSerializer getStackTraceElementSerializer()
      Returns the global stack trace serializer implementation which is used to serialize StackTraceElement objects in getStackTraceString methods.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable)
      Returns the smart stack trace for the given throwable.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, boolean ignoreAllCauses)
      Returns the smart stack trace for the given throwable.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, int maxDepth)
      Returns the smart stack trace for the given throwable using elements found until the maxDepth.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, int maxDepth, boolean ignoreAllCauses)
      Returns the smart stack trace for the given throwable using elements found until the maxDepth.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, int maxDepth, boolean ignoreAllCauses, boolean printPackageInformation)
      Returns the smart stack trace for the given throwable using elements found until the maxDepth.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, java.lang.String rootPackage)
      Returns the smart stack trace for the given throwable using root package provided.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, java.lang.String rootPackage, java.lang.String groupPackage)
      Returns the smart stack trace for the given throwable using root package provided.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, java.util.Set<java.lang.String> rootPackageSet, java.util.Set<java.lang.String> groupPackageSet, java.util.Set<java.lang.String> ignorePackageSet)
      Returns the smart stack trace for the given throwable using packages provided.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, java.util.Set<java.lang.String> rootPackageSet, java.util.Set<java.lang.String> groupPackageSet, java.util.Set<java.lang.String> ignorePackageSet, boolean ignoreAllCauses)
      Returns the smart stack trace for the given throwable using packages provided.
      static java.lang.String getStackTraceString​(java.lang.Throwable throwable, java.util.Set<java.lang.String> rootPackageSet, java.util.Set<java.lang.String> groupPackageSet, java.util.Set<java.lang.String> ignorePackageSet, boolean ignoreAllCauses, boolean printPackageInformation)
      Returns the smart stack trace for the given throwable using packages provided.
      static boolean isPrintPackageInformation()
      Returns the value of print package information option.
      static void registerGroupPackage​(java.lang.String packageString)
      Registers a new group package.
      static void registerIgnorePackage​(java.lang.String packageString, boolean ignoreCauseClasses)
      Registers a new ignore package.
      static void registerRootPackage​(java.lang.String packageString)
      Registers a new root package.
      static java.lang.Throwable searchCause​(java.lang.Throwable throwable, java.lang.Class<?> causeClass)
      Searches for the given cause class in the exception chain of the throwable.
      static java.lang.Throwable searchCause​(java.lang.Throwable throwable, java.lang.Class<?> causeClass, int maxDepth)
      Searches for the given cause class in the exception chain of the throwable.
      static java.lang.Throwable searchCause​(java.lang.Throwable throwable, java.lang.Class<?> causeClass, java.lang.String causeMessage)
      Searches for the given cause class and cause message in the exception chain of the throwable.
      static java.lang.Throwable searchCause​(java.lang.Throwable throwable, java.lang.Class<?> causeClass, java.lang.String causeMessage, int maxDepth)
      Searches for the given cause class and cause message in the exception chain of the throwable.
      static void setIgnoreAllCauses​(boolean ignoreAllCauses)
      Sets the value of ignore all causes option.
      static void setPrintPackageInformation​(boolean printPackageInformation)
      Sets the value of print package information option.
      static void setStackTraceElementSerializer​(com.arthenica.smartexception.StackTraceElementSerializer stackTraceElementSerializer)
      Sets the global stack trace serializer implementation which is used to serialize StackTraceElement objects in getStackTraceString methods.
      • Methods inherited from class java.lang.Object

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

      • Exceptions

        public Exceptions()
    • Method Detail

      • registerRootPackage

        public static void registerRootPackage​(java.lang.String packageString)

        Registers a new root package.

        Parameters:
        packageString - root package name to register
      • clearRootPackages

        public static void clearRootPackages()

        Clears previously registered root packages.

      • registerGroupPackage

        public static void registerGroupPackage​(java.lang.String packageString)

        Registers a new group package.

        Parameters:
        packageString - group package name to register
      • clearGroupPackages

        public static void clearGroupPackages()

        Clears previously registered group packages.

      • getStackTraceElementSerializer

        public static com.arthenica.smartexception.StackTraceElementSerializer getStackTraceElementSerializer()

        Returns the global stack trace serializer implementation which is used to serialize StackTraceElement objects in getStackTraceString methods.

        Returns:
        current stack trace serializer implementation
      • setStackTraceElementSerializer

        public static void setStackTraceElementSerializer​(com.arthenica.smartexception.StackTraceElementSerializer stackTraceElementSerializer)

        Sets the global stack trace serializer implementation which is used to serialize StackTraceElement objects in getStackTraceString methods.

        Parameters:
        stackTraceElementSerializer - new stack trace serializer implementation
      • registerIgnorePackage

        public static void registerIgnorePackage​(java.lang.String packageString,
                                                 boolean ignoreCauseClasses)

        Registers a new ignore package.

        Parameters:
        packageString - ignore package name to register
        ignoreCauseClasses - ignore cause classes from this package too
      • clearIgnorePackages

        public static void clearIgnorePackages()

        Clears previously registered ignore packages.

      • getIgnoreAllCauses

        public static boolean getIgnoreAllCauses()

        Returns the value of ignore all causes option.

        Returns:
        the value of global ignore all causes option. If value is true then stack trace elements printed or converted to string will not include causes. If value is false causes will be appended to the stack trace of the main throwable.
      • setIgnoreAllCauses

        public static void setIgnoreAllCauses​(boolean ignoreAllCauses)

        Sets the value of ignore all causes option.

        Parameters:
        ignoreAllCauses - new global ignore all causes option. If value is true then stack trace elements printed or converted to string will not include causes. If value is false causes will be appended to the stack trace of the main throwable.
      • isPrintPackageInformation

        public static boolean isPrintPackageInformation()

        Returns the value of print package information option.

        Returns:
        the value of global print package information option. When this option is true, stack trace elements printed or converted to string will include the name of the jar file that includes the printed class and the version of the jar. If it is false, none of this information is printed
      • setPrintPackageInformation

        public static void setPrintPackageInformation​(boolean printPackageInformation)

        Sets the value of print package information option.

        When this option is true, stack trace elements printed or converted to string will include the name of the jar file that includes the printed class and the version of the jar.

        Note that for some libraries extracting the jar file and the version may not be possible.

        Parameters:
        printPackageInformation - new print package information option.
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable)

        Returns the smart stack trace for the given throwable.

        This method uses root packages registered by registerRootPackage(String), group packages registered by registerGroupPackage(String) and ignore packages registered by registerIgnorePackage(String, boolean) to build the smart stack trace.

        Parameters:
        throwable - parent throwable
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           boolean ignoreAllCauses)

        Returns the smart stack trace for the given throwable.

        This method uses root packages registered by registerRootPackage(String), group packages registered by registerGroupPackage(String) and ignore packages registered by registerIgnorePackage(String, boolean) to build the smart stack trace.

        Parameters:
        throwable - parent throwable
        ignoreAllCauses - ignore all causes in the exception chain
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           java.util.Set<java.lang.String> rootPackageSet,
                                                           java.util.Set<java.lang.String> groupPackageSet,
                                                           java.util.Set<java.lang.String> ignorePackageSet)

        Returns the smart stack trace for the given throwable using packages provided.

        Parameters:
        throwable - parent throwable
        rootPackageSet - root packages to use for building the stack trace
        groupPackageSet - group packages to use for building the stack trace
        ignorePackageSet - ignore packages to use for building the stack trace
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           java.util.Set<java.lang.String> rootPackageSet,
                                                           java.util.Set<java.lang.String> groupPackageSet,
                                                           java.util.Set<java.lang.String> ignorePackageSet,
                                                           boolean ignoreAllCauses)

        Returns the smart stack trace for the given throwable using packages provided.

        Parameters:
        throwable - parent throwable
        rootPackageSet - root packages to use for building the stack trace
        groupPackageSet - group packages to use for building the stack trace
        ignorePackageSet - ignore packages to use for building the stack trace
        ignoreAllCauses - ignore all causes in the exception chain
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           java.util.Set<java.lang.String> rootPackageSet,
                                                           java.util.Set<java.lang.String> groupPackageSet,
                                                           java.util.Set<java.lang.String> ignorePackageSet,
                                                           boolean ignoreAllCauses,
                                                           boolean printPackageInformation)

        Returns the smart stack trace for the given throwable using packages provided.

        Parameters:
        throwable - parent throwable
        rootPackageSet - root packages to use for building the stack trace
        groupPackageSet - group packages to use for building the stack trace
        ignorePackageSet - ignore packages to use for building the stack trace
        ignoreAllCauses - ignore all causes in the exception chain
        printPackageInformation - print package information
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           java.lang.String rootPackage)

        Returns the smart stack trace for the given throwable using root package provided.

        Parameters:
        throwable - parent throwable
        rootPackage - root package to use for building the stack trace
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           java.lang.String rootPackage,
                                                           java.lang.String groupPackage)

        Returns the smart stack trace for the given throwable using root package provided.

        Parameters:
        throwable - parent throwable
        rootPackage - root package to use for building the stack trace
        groupPackage - group package to use for building the stack trace
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           int maxDepth)

        Returns the smart stack trace for the given throwable using elements found until the maxDepth.

        Parameters:
        throwable - parent throwable
        maxDepth - max depth in exception chain that will be used
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           int maxDepth,
                                                           boolean ignoreAllCauses)

        Returns the smart stack trace for the given throwable using elements found until the maxDepth.

        Parameters:
        throwable - parent throwable
        maxDepth - max depth in exception chain that will be used
        ignoreAllCauses - ignore all causes in the exception chain
        Returns:
        a string containing the smart stack trace for the given throwable
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable throwable,
                                                           int maxDepth,
                                                           boolean ignoreAllCauses,
                                                           boolean printPackageInformation)

        Returns the smart stack trace for the given throwable using elements found until the maxDepth.

        Parameters:
        throwable - parent throwable
        maxDepth - max depth in exception chain that will be used
        ignoreAllCauses - ignore all causes in the exception chain
        printPackageInformation - print package information
        Returns:
        a string containing the smart stack trace for the given throwable
      • getAllMessages

        public static java.lang.String getAllMessages​(java.lang.Throwable throwable)

        Returns all messages found in the exception chain of the throwable as a single string.

        Parameters:
        throwable - parent throwable
        Returns:
        a string containing all messages found in the exception chain
      • containsCause

        public static boolean containsCause​(java.lang.Throwable throwable,
                                            java.lang.Class<?> causeClass)

        Returns true if the given cause class is found in the exception chain of the throwable.

        Search starts from the throwable itself and goes through the exception chain up until AbstractExceptions.DEFAULT_MAX_DEPTH.

        Parameters:
        throwable - parent throwable
        causeClass - class to search for
        Returns:
        true if the given cause class is found in the exception chain of the throwable, false otherwise
      • containsCause

        public static boolean containsCause​(java.lang.Throwable throwable,
                                            java.lang.Class<?> causeClass,
                                            java.lang.String causeMessage)

        Returns true if the given cause class and cause message is found in the exception chain of the throwable.

        Search starts from the throwable itself and goes through the exception chain up until AbstractExceptions.DEFAULT_MAX_DEPTH.

        Parameters:
        throwable - parent throwable
        causeClass - class to search for
        causeMessage - message to search for
        Returns:
        true if the given cause class and cause message is found in the exception chain of the throwable, false otherwise
      • getCause

        public static java.lang.Throwable getCause​(java.lang.Throwable throwable)

        Returns the cause of the throwable by walking through the exception chain.

        Parameters:
        throwable - parent throwable
        Returns:
        the cause of the throwable found or null if throwable does not have a cause
      • getCause

        public static java.lang.Throwable getCause​(java.lang.Throwable throwable,
                                                   int maxDepth)

        Returns the cause of the throwable by walking through the exception chain.

        Note that this method walks through the exception chain up to given maxDepth. If exception chain includes more items than maxDepth, cause found at maxDepth level is returned.

        Parameters:
        throwable - parent throwable
        maxDepth - max depth in exception chain that will be searched
        Returns:
        the cause of the throwable found until maxDepth is reached in the exception chain or null if throwable does not have a cause
      • searchCause

        public static java.lang.Throwable searchCause​(java.lang.Throwable throwable,
                                                      java.lang.Class<?> causeClass)

        Searches for the given cause class in the exception chain of the throwable.

        Search starts from the throwable itself and goes through the exception chain up until AbstractExceptions.DEFAULT_MAX_DEPTH.

        Parameters:
        throwable - parent throwable
        causeClass - class to search for
        Returns:
        the throwable found or null if no class in exception chain matches the given cause class
      • searchCause

        public static java.lang.Throwable searchCause​(java.lang.Throwable throwable,
                                                      java.lang.Class<?> causeClass,
                                                      java.lang.String causeMessage)

        Searches for the given cause class and cause message in the exception chain of the throwable.

        Search starts from the throwable itself and goes through the exception chain up until AbstractExceptions.DEFAULT_MAX_DEPTH.

        Parameters:
        throwable - parent throwable
        causeClass - class to search for
        causeMessage - message to search for
        Returns:
        the throwable found or null if no class in exception chain matches the given cause class and cause message
      • searchCause

        public static java.lang.Throwable searchCause​(java.lang.Throwable throwable,
                                                      java.lang.Class<?> causeClass,
                                                      java.lang.String causeMessage,
                                                      int maxDepth)

        Searches for the given cause class and cause message in the exception chain of the throwable.

        Search starts from the throwable itself and goes through the exception chain up until the given maxDepth.

        Parameters:
        throwable - parent throwable
        causeClass - class to search for
        causeMessage - message to search for
        maxDepth - max depth in exception chain that will be searched
        Returns:
        the throwable found or null if no class in exception chain matches the given cause class and cause message
      • searchCause

        public static java.lang.Throwable searchCause​(java.lang.Throwable throwable,
                                                      java.lang.Class<?> causeClass,
                                                      int maxDepth)

        Searches for the given cause class in the exception chain of the throwable.

        Search starts from the throwable itself and goes through the exception chain up until the given maxDepth.

        Parameters:
        throwable - parent throwable
        causeClass - class to search for
        maxDepth - max depth in exception chain that will be searched
        Returns:
        the throwable found or null if no class in exception chain matches the given cause class