public final class Throwables
extends java.lang.Object
Throwable| Modifier and Type | Method and Description |
|---|---|
static java.util.List<java.lang.Throwable> |
getCausalChain(java.lang.Throwable throwable)
Gets a
Throwable cause chain as a list. |
static java.lang.Throwable |
getRootCause(java.lang.Throwable throwable)
Returns the innermost cause of
throwable. |
static java.lang.String |
getStackTraceAsString(java.lang.Throwable throwable)
Returns a string containing the result of
toString(), followed by the full,
recursive stack trace of throwable. |
static <X extends java.lang.Throwable> |
throwIfInstanceOf(java.lang.Throwable throwable,
java.lang.Class<X> declaredType)
Throws
throwable if it is an instance of declaredType |
static void |
throwIfUnchecked(java.lang.Throwable throwable)
Throws
throwable if it is a RuntimeException or Error |
public static <X extends java.lang.Throwable> void throwIfInstanceOf(java.lang.Throwable throwable,
java.lang.Class<X> declaredType)
throws X extends java.lang.Throwable
throwable if it is an instance of declaredTypeX - The type to check against the provided throwablethrowable - The throwable to type check and throw if an instance of specified typedeclaredType - The type to check against the provided throwableX - If the provided throwable is an instance of XX extends java.lang.Throwablepublic static void throwIfUnchecked(java.lang.Throwable throwable)
throwable if it is a RuntimeException or Errorthrowable - The throwable to type check and throw if an instance of a runtime issuepublic static java.lang.Throwable getRootCause(java.lang.Throwable throwable)
throwable. The first throwable in a chain provides context from when the
error or exception was initially detected
assertEquals("Unable to assign a customer id", Throwables.getRootCause(e).getMessage());
throwable - The throwable to walk the cause trace of and find the rootpublic static java.util.List<java.lang.Throwable> getCausalChain(java.lang.Throwable throwable)
Throwable cause chain as a list. The first entry in the list will be throwable followed by its cause hierarchy. Note that this is a snapshot of the cause chain and will not reflect
any subsequent changes to the cause chain.throwable - The Throwable to extract causes fromthrowablepublic static java.lang.String getStackTraceAsString(java.lang.Throwable throwable)
toString(), followed by the full,
recursive stack trace of throwable. Note that you probably should not be parsing the resulting string; if
you need programmatic access to the stack frames, call Throwable.getStackTrace()throwable - Representation of the error to get stack trace information from