Class Exceptions
- java.lang.Object
-
- com.arthenica.smartexception.java.Exceptions
-
public class Exceptions extends java.lang.ObjectUtility 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 voidclearGroupPackages()Clears previously registered group packages.static voidclearIgnorePackages()Clears previously registered ignore packages.static voidclearRootPackages()Clears previously registered root packages.static booleancontainsCause(java.lang.Throwable throwable, java.lang.Class<?> causeClass)Returns true if the given cause class is found in the exception chain of thethrowable.static booleancontainsCause(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 thethrowable.static java.lang.StringgetAllMessages(java.lang.Throwable throwable)Returns all messages found in the exception chain of thethrowableas a single string.static java.lang.ThrowablegetCause(java.lang.Throwable throwable)Returns the cause of thethrowableby walking through the exception chain.static java.lang.ThrowablegetCause(java.lang.Throwable throwable, int maxDepth)Returns the cause of thethrowableby walking through the exception chain.static booleangetIgnoreAllCauses()Returns the value of ignore all causes option.static com.arthenica.smartexception.StackTraceElementSerializergetStackTraceElementSerializer()Returns the global stack trace serializer implementation which is used to serializeStackTraceElementobjects ingetStackTraceStringmethods.static java.lang.StringgetStackTraceString(java.lang.Throwable throwable)Returns the smart stack trace for the giventhrowable.static java.lang.StringgetStackTraceString(java.lang.Throwable throwable, boolean ignoreAllCauses)Returns the smart stack trace for the giventhrowable.static java.lang.StringgetStackTraceString(java.lang.Throwable throwable, int maxDepth)Returns the smart stack trace for the giventhrowableusing elements found until the maxDepth.static java.lang.StringgetStackTraceString(java.lang.Throwable throwable, int maxDepth, boolean ignoreAllCauses)Returns the smart stack trace for the giventhrowableusing elements found until the maxDepth.static java.lang.StringgetStackTraceString(java.lang.Throwable throwable, int maxDepth, boolean ignoreAllCauses, boolean printPackageInformation)Returns the smart stack trace for the giventhrowableusing elements found until the maxDepth.static java.lang.StringgetStackTraceString(java.lang.Throwable throwable, java.lang.String rootPackage)Returns the smart stack trace for the giventhrowableusing root package provided.static java.lang.StringgetStackTraceString(java.lang.Throwable throwable, java.lang.String rootPackage, java.lang.String groupPackage)Returns the smart stack trace for the giventhrowableusing root package provided.static java.lang.StringgetStackTraceString(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 giventhrowableusing packages provided.static java.lang.StringgetStackTraceString(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 giventhrowableusing packages provided.static java.lang.StringgetStackTraceString(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 giventhrowableusing packages provided.static booleanisPrintPackageInformation()Returns the value of print package information option.static voidregisterGroupPackage(java.lang.String packageString)Registers a new group package.static voidregisterIgnorePackage(java.lang.String packageString, boolean ignoreCauseClasses)Registers a new ignore package.static voidregisterRootPackage(java.lang.String packageString)Registers a new root package.static java.lang.ThrowablesearchCause(java.lang.Throwable throwable, java.lang.Class<?> causeClass)Searches for the given cause class in the exception chain of thethrowable.static java.lang.ThrowablesearchCause(java.lang.Throwable throwable, java.lang.Class<?> causeClass, int maxDepth)Searches for the given cause class in the exception chain of thethrowable.static java.lang.ThrowablesearchCause(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 thethrowable.static java.lang.ThrowablesearchCause(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 thethrowable.static voidsetIgnoreAllCauses(boolean ignoreAllCauses)Sets the value of ignore all causes option.static voidsetPrintPackageInformation(boolean printPackageInformation)Sets the value of print package information option.static voidsetStackTraceElementSerializer(com.arthenica.smartexception.StackTraceElementSerializer stackTraceElementSerializer)Sets the global stack trace serializer implementation which is used to serializeStackTraceElementobjects ingetStackTraceStringmethods.
-
-
-
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
StackTraceElementobjects ingetStackTraceStringmethods.- 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
StackTraceElementobjects ingetStackTraceStringmethods.- 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 registerignoreCauseClasses- 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 byregisterGroupPackage(String)and ignore packages registered byregisterIgnorePackage(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 byregisterGroupPackage(String)and ignore packages registered byregisterIgnorePackage(String, boolean)to build the smart stack trace.- Parameters:
throwable- parent throwableignoreAllCauses- 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
throwableusing packages provided.- Parameters:
throwable- parent throwablerootPackageSet- root packages to use for building the stack tracegroupPackageSet- group packages to use for building the stack traceignorePackageSet- 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
throwableusing packages provided.- Parameters:
throwable- parent throwablerootPackageSet- root packages to use for building the stack tracegroupPackageSet- group packages to use for building the stack traceignorePackageSet- ignore packages to use for building the stack traceignoreAllCauses- 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
throwableusing packages provided.- Parameters:
throwable- parent throwablerootPackageSet- root packages to use for building the stack tracegroupPackageSet- group packages to use for building the stack traceignorePackageSet- ignore packages to use for building the stack traceignoreAllCauses- ignore all causes in the exception chainprintPackageInformation- 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
throwableusing root package provided.- Parameters:
throwable- parent throwablerootPackage- 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
throwableusing root package provided.- Parameters:
throwable- parent throwablerootPackage- root package to use for building the stack tracegroupPackage- 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
throwableusing elements found until the maxDepth.- Parameters:
throwable- parent throwablemaxDepth- 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
throwableusing elements found until the maxDepth.- Parameters:
throwable- parent throwablemaxDepth- max depth in exception chain that will be usedignoreAllCauses- 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
throwableusing elements found until the maxDepth.- Parameters:
throwable- parent throwablemaxDepth- max depth in exception chain that will be usedignoreAllCauses- ignore all causes in the exception chainprintPackageInformation- 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
throwableas 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
throwableitself and goes through the exception chain up untilAbstractExceptions.DEFAULT_MAX_DEPTH.- Parameters:
throwable- parent throwablecauseClass- 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
throwableitself and goes through the exception chain up untilAbstractExceptions.DEFAULT_MAX_DEPTH.- Parameters:
throwable- parent throwablecauseClass- class to search forcauseMessage- 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
throwableby walking through the exception chain.- Parameters:
throwable- parent throwable- Returns:
- the cause of the
throwablefound or null ifthrowabledoes not have a cause
-
getCause
public static java.lang.Throwable getCause(java.lang.Throwable throwable, int maxDepth)Returns the cause of the
throwableby walking through the exception chain.
Note that this method walks through the exception chain up to given
maxDepth. If exception chain includes more items thanmaxDepth, cause found atmaxDepthlevel is returned.- Parameters:
throwable- parent throwablemaxDepth- max depth in exception chain that will be searched- Returns:
- the cause of the
throwablefound untilmaxDepthis reached in the exception chain or null ifthrowabledoes 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
throwableitself and goes through the exception chain up untilAbstractExceptions.DEFAULT_MAX_DEPTH.- Parameters:
throwable- parent throwablecauseClass- class to search for- Returns:
- the
throwablefound 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
throwableitself and goes through the exception chain up untilAbstractExceptions.DEFAULT_MAX_DEPTH.- Parameters:
throwable- parent throwablecauseClass- class to search forcauseMessage- message to search for- Returns:
- the
throwablefound 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
throwableitself and goes through the exception chain up until the givenmaxDepth.- Parameters:
throwable- parent throwablecauseClass- class to search forcauseMessage- message to search formaxDepth- max depth in exception chain that will be searched- Returns:
- the
throwablefound 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
throwableitself and goes through the exception chain up until the givenmaxDepth.- Parameters:
throwable- parent throwablecauseClass- class to search formaxDepth- max depth in exception chain that will be searched- Returns:
- the
throwablefound or null if no class in exception chain matches the given cause class
-
-