public class ExceptionUtils extends Object
ExceptionUtils
provides utilities for manipulating
Throwable
objects.
Modifier and Type | Field and Description |
---|---|
protected static String[] |
CAUSE_METHOD_NAMES
The names of methods commonly used to access a wrapped
exception.
|
Modifier | Constructor and Description |
---|---|
protected |
ExceptionUtils()
Constructs a new
ExceptionUtils . |
Modifier and Type | Method and Description |
---|---|
static void |
addCauseMethodName(String methodName)
Adds to the list of method names used in the search for
Throwable
objects. |
static Throwable |
getCause(Throwable throwable)
Introspects the specified
Throwable to obtain the cause. |
static Throwable |
getCause(Throwable throwable,
String[] methodNames)
Introspects the specified
Throwable to obtain the cause
using a supplied array of method names. |
static String |
getFullStackTrace(Throwable t)
A way to get the entire nested stack-trace of an throwable.
|
static Throwable |
getRootCause(Throwable throwable)
Walks through the exception chain to the last element -- the
"root" of the tree -- using
getCause(Throwable) , and
returns that exception. |
static String[] |
getRootCauseStackTrace(Throwable t)
Creates a compact stack trace for the root cause of the supplied
throwable.
|
static String[] |
getStackFrames(Throwable t)
Captures the stack trace associated with the specified
Throwable object, decomposing it into a list of
stack frames. |
static String |
getStackTrace(Throwable t)
A convenient way of extracting the stack trace from an
exception.
|
static int |
getThrowableCount(Throwable throwable)
Returns the number of
Throwable objects in the
exception chain. |
static Throwable[] |
getThrowables(Throwable throwable)
Returns the list of
Throwable objects in the
exception chain. |
static int |
indexOfThrowable(Throwable throwable,
Class type)
Delegates to
indexOfThrowable(Throwable, Class, int) ,
starting the search at the beginning of the exception chain. |
static int |
indexOfThrowable(Throwable throwable,
Class type,
int fromIndex)
Returns the (zero based) index, of the first
Throwable that matches the specified type in the
exception chain of Throwable objects with an index
greater than or equal to the specified index, or
-1 if the type is not found. |
static boolean |
isNestedThrowable(Throwable throwable)
Whether an Throwable is considered nested or not.
|
static void |
printRootCauseStackTrace(Throwable t)
Equivalent to printRootCauseStackTrace(t, System.err)
|
static void |
printRootCauseStackTrace(Throwable t,
PrintStream stream)
Prints a compact stack trace for the root cause of a throwable.
|
static void |
printRootCauseStackTrace(Throwable t,
PrintWriter writer)
Same as printRootCauseStackTrace(t, stream), except it takes
a PrintWriter as an argument.
|
protected static String[] CAUSE_METHOD_NAMES
protected ExceptionUtils()
ExceptionUtils
. Protected to
discourage instantiation.public static void addCauseMethodName(String methodName)
Adds to the list of method names used in the search for Throwable
objects.
methodName
- the methodName to add to the list, null and empty strings are ignoredpublic static Throwable getCause(Throwable throwable)
Introspects the specified Throwable
to obtain the cause.
The method searches for methods with specific names that return a
Throwable
object. This will pick up most wrapping exceptions,
including those from JDK 1.4, and
The method names can be added to using addCauseMethodName(String)
.
The default list searched for are:
getCause()
getNextException()
getTargetException()
getException()
getSourceException()
getRootCause()
getCausedByException()
getNested()
In the absence of any such method, the object is inspected for a
detail
field assignable to a Throwable
.
If none of the above is found, returns null
.
throwable
- The exception to introspect for a cause.Throwable
.NullPointerException
- if the throwable is nullpublic static Throwable getCause(Throwable throwable, String[] methodNames)
Introspects the specified Throwable
to obtain the cause
using a supplied array of method names.
throwable
- The exception to introspect for a cause.Throwable
.NullPointerException
- if the method names array is null or contains nullNullPointerException
- if the throwable is nullpublic static Throwable getRootCause(Throwable throwable)
Walks through the exception chain to the last element -- the
"root" of the tree -- using getCause(Throwable)
, and
returns that exception.
throwable
- the throwable to get the root cause forThrowable
.public static int getThrowableCount(Throwable throwable)
Returns the number of Throwable
objects in the
exception chain.
throwable
- the exception to inspectpublic static Throwable[] getThrowables(Throwable throwable)
Returns the list of Throwable
objects in the
exception chain.
throwable
- the exception to inspectThrowable
objects.public static int indexOfThrowable(Throwable throwable, Class type)
Delegates to indexOfThrowable(Throwable, Class, int)
,
starting the search at the beginning of the exception chain.
public static int indexOfThrowable(Throwable throwable, Class type, int fromIndex)
Returns the (zero based) index, of the first
Throwable
that matches the specified type in the
exception chain of Throwable
objects with an index
greater than or equal to the specified index, or
-1
if the type is not found.
throwable
- the exception to inspecttype
- Class
to look forfromIndex
- the (zero based) index of the starting
position in the chain to be searched-1
if the type is not foundIndexOutOfBoundsException
- If the fromIndex
argument is negative or not less than the count of
Throwable
s in the chain.public static void printRootCauseStackTrace(Throwable t, PrintStream stream)
The method is equivalent to t.printStackTrace() for throwables that don't have nested causes.
public static void printRootCauseStackTrace(Throwable t)
public static void printRootCauseStackTrace(Throwable t, PrintWriter writer)
public static String[] getRootCauseStackTrace(Throwable t)
printRootCauseStackTrace(Throwable t, PrintStream s)
public static String getStackTrace(Throwable t)
t
- The Throwable
.printStackTrace(PrintWriter)
method.public static String getFullStackTrace(Throwable t)
t
- The Throwable
.public static boolean isNestedThrowable(Throwable throwable)
throwable
- The Throwable
.Copyright © 2001-2013 Codehaus. All Rights Reserved.