Top Description Fields Constructors Methods
org.apache.commons.lang3.exception

public Class ExceptionUtils

extends Object
Class Inheritance
Imports
java.io.PrintStream, .PrintWriter, .StringWriter, java.lang.reflect.Method, .UndeclaredThrowableException, java.util.ArrayList, .Collections, .List, .Objects, .StringTokenizer, java.util.function.Consumer, java.util.stream.Stream, org.apache.commons.lang3.ArrayUtils, .ClassUtils, .StringUtils, org.apache.commons.lang3.reflect.MethodUtils, org.apache.commons.lang3.util.IterableStringTokenizer

Provides utilities for manipulating and examining Throwable objects.
Since
1.0

Field Summary

Modifier and TypeField and Description
private static final String[]
CAUSE_METHOD_NAMES

The names of methods commonly used to access a wrapped exception.

private static final int
pack-priv static final String
WRAPPED_MARKER

Used when printing stack frames to denote the start of a wrapped exception.

Constructor Summary

AccessConstructor and Description
public
ExceptionUtils()
Deprecated TODO Make private in 4.0.

Public constructor allows an instance of ExceptionUtils to be created, although that is not normally necessary.

Method Summary

Modifier and TypeMethod and Description
public static <
The type of the returned value.
T extends RuntimeException
>
T

Returns:

Never actually returned, this generic type matches any type which the calling site requires. "Returning" the results of this method, as done in the propagateExample above, will satisfy the Java compiler requirement that all code paths return a value.
asRuntimeException
(final Throwable
The throwable to rethrow.
throwable
)

Throws the given (usually checked) exception without adding the exception to the throws clause of the calling method.

private static <R, T extends Throwable> R
eraseType(final Throwable throwable)

Claims a Throwable is another Throwable type using type erasure.

public static void
forEach(final Throwable
The Throwable to traverse.
throwable
,
final Consumer<Throwable>
a non-interfering action to perform on the elements.
consumer
)

Performs an action for each Throwable causes of the given Throwable.

public static Throwable

Returns:

the cause of the Throwable, null if none found or null throwable input.
getCause
(final Throwable
the throwable to introspect for a cause, may be null.
throwable
)
Deprecated This feature will be removed in Lang 4, use Throwable#getCause instead.

Introspects the Throwable to obtain the cause.

public static Throwable

Returns:

the cause of the Throwable, null if none found or null throwable input.
getCause
(final Throwable
the throwable to introspect for a cause, may be null.
throwable
,
String[]
the method names, null treated as default set.
methodNames
)
Deprecated This feature will be removed in Lang 4, use Throwable#getCause instead.

Introspects the Throwable to obtain the cause.

private static Throwable

Returns:

the wrapped exception, or null if not found.
getCauseUsingMethodName
(final Throwable
the exception to examine.
throwable
,
final String
the name of the method to find and invoke.
methodName
)

Gets a Throwable by method name.

public static String[]

Returns:

cloned array of the default method names.
getDefaultCauseMethodNames
()
Deprecated This feature will be removed in Lang 4.

Gets the default names used when searching for the cause of an exception.

public static String

Returns:

the message, non-null.
getMessage
(final Throwable
the throwable to get a message for, null returns empty string.
th
)

Gets a short message summarizing the exception.

public static Throwable

Returns:

the root cause of the Throwable, null if null throwable input.
getRootCause
(final Throwable
the throwable to get the root cause for, may be null.
throwable
)

Walks the Throwable to obtain its root cause.

public static String

Returns:

the message, non-null.
getRootCauseMessage
(final Throwable
the throwable to get a message for, null returns empty string.
throwable
)

Gets a short message summarizing the root cause exception.

public static String[]

Returns:

an array of stack trace frames, never null.
getRootCauseStackTrace
(final Throwable
the throwable to examine, may be null.
throwable
)

Gets a compact stack trace for the root cause of the supplied Throwable.

public static List<String>

Returns:

a list of stack trace frames, never null.
getRootCauseStackTraceList
(final Throwable
the throwable to examine, may be null.
throwable
)

Gets a compact stack trace for the root cause of the supplied Throwable.

pack-priv static List<String>

Returns:

List of stack frames.
getStackFrameList
(final Throwable
is any throwable.
throwable
)

Gets a List of stack frames, the message is not included.

pack-priv static String[]

Returns:

an array where each element is a line from the argument.
getStackFrames
(final String
a stack trace String.
stackTrace
)

Gets an array where each element is a line from the argument.

public static String[]

Returns:

an array of strings describing each stack frame, never null.
getStackFrames
(final Throwable
the Throwable to examine, may be null.
throwable
)

Gets the stack trace associated with the specified Throwable object, decomposing it into a list of stack frames.

public static String

Returns:

the stack trace as generated by the exception's printStackTrace(PrintWriter) method, or an empty String if null input.
getStackTrace
(final Throwable
the Throwable to be examined, may be null.
throwable
)

Gets the stack trace from a Throwable as a String, including suppressed and cause exceptions.

public static int

Returns:

the count of throwables, zero on null input.
getThrowableCount
(final Throwable
the throwable to inspect, may be null.
throwable
)

Gets a count of the number of Throwable objects in the exception chain.

public static List<Throwable>

Returns:

the list of throwables, never null.
getThrowableList
(Throwable
the throwable to inspect, may be null.
throwable
)

Gets the list of Throwable objects in the exception chain.

public static Throwable[]

Returns:

the array of throwables, never null.
getThrowables
(final Throwable
the throwable to inspect, may be null.
throwable
)

Gets the list of Throwable objects in the exception chain.

public static boolean

Returns:

true, if chain is an instance of type or is an UndeclaredThrowableException wrapping a cause.
hasCause
(Throwable
The root of a Throwable causal chain.
chain
,
final Class<? extends Throwable>
The exception type to test.
type
)

Tests if the throwable's causal chain have an immediate or wrapped exception of the given type?

private static int

Returns:

index of the type within throwables nested within the specified throwable.
indexOf
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<? extends Throwable>
the type to search for, subclasses match, null returns -1.
type
,
int
the (zero-based) index of the starting position, negative treated as zero, larger than chain size returns -1.
fromIndex
,
final boolean
if true, compares with Class#isAssignableFrom(Class), otherwise compares using references.
subclass
)

Worker method for the indexOfType methods.

public static int

Returns:

the index into the throwable chain, -1 if no match or null input.
indexOfThrowable
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<? extends Throwable>
the class to search for, subclasses do not match, null returns -1.
clazz
)

Returns the (zero-based) index of the first Throwable that matches the specified class (exactly) in the exception chain.

public static int

Returns:

the index into the throwable chain, -1 if no match or null input.
indexOfThrowable
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<? extends Throwable>
the class to search for, subclasses do not match, null returns -1.
clazz
,
final int
the (zero-based) index of the starting position, negative treated as zero, larger than chain size returns -1.
fromIndex
)

Returns the (zero-based) index of the first Throwable that matches the specified type in the exception chain from a specified index.

public static int

Returns:

the index into the throwable chain, -1 if no match or null input.
indexOfType
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<? extends Throwable>
the type to search for, subclasses match, null returns -1.
type
)

Returns the (zero-based) index of the first Throwable that matches the specified class or subclass in the exception chain.

public static int

Returns:

the index into the throwable chain, -1 if no match or null input.
indexOfType
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<? extends Throwable>
the type to search for, subclasses match, null returns -1.
type
,
final int
the (zero-based) index of the starting position, negative treated as zero, larger than chain size returns -1.
fromIndex
)

Returns the (zero-based) index of the first Throwable that matches the specified type in the exception chain from a specified index.

public static boolean

Returns:

True if the given Throwable is a checked exception.
isChecked
(final Throwable
The throwable to check.
throwable
)

Checks if a throwable represents a checked exception

public static boolean

Returns:

True if the given Throwable is an unchecked exception.
isUnchecked
(final Throwable
The throwable to check.
throwable
)

Checks if a throwable represents an unchecked exception

public static void
printRootCauseStackTrace(final Throwable
the throwable to output.
throwable
)

Prints a compact stack trace for the root cause of a throwable to System.err.

public static void
printRootCauseStackTrace(final Throwable
the throwable to output, may be null.
throwable
,
final PrintStream
the stream to output to, may not be null.
printStream
)

Prints a compact stack trace for the root cause of a throwable.

public static void
printRootCauseStackTrace(final Throwable
the throwable to output, may be null.
throwable
,
final PrintWriter
the writer to output to, may not be null.
printWriter
)

Prints a compact stack trace for the root cause of a throwable.

public static void
removeCommonFrames(final List<String>
stack trace of a cause throwable.
causeFrames
,
final List<String>
stack trace of a wrapper throwable.
wrapperFrames
)

Removes common frames from the cause trace given the two stack traces.

public static <
The type of the return value.
T
>
T

Returns:

Never actually returns, this generic type matches any type which the calling site requires. "Returning" the results of this method, as done in the propagateExample above, will satisfy the Java compiler requirement that all code paths return a value.
rethrow
(final Throwable
The throwable to rethrow.
throwable
)

Throws the given (usually checked) exception without adding the exception to the throws clause of the calling method.

public static Stream<Throwable>

Returns:

A new Stream of Throwable causes.
stream
(final Throwable
The Throwable to traverse.
throwable
)

Streams causes of a Throwable.

private static <
the type of Throwable you are searching.
T extends Throwable
>
T

Returns:

throwable of the type within throwables nested within the specified throwable.
throwableOf
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<T>
the type to search, subclasses match, null returns null.
type
,
int
the (zero-based) index of the starting position, negative treated as zero, larger than chain size returns null.
fromIndex
,
final boolean
if true, compares with Class#isAssignableFrom(Class), otherwise compares using references.
subclass
)

Worker method for the throwableOfType methods.

public static <
the type of Throwable you are searching.
T extends Throwable
>
T

Returns:

the first matching throwable from the throwable chain, null if no match or null input.
throwableOfThrowable
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<T>
the class to search for, subclasses do not match, null returns null.
clazz
)

Returns the first Throwable that matches the specified class (exactly) in the exception chain.

public static <
the type of Throwable you are searching.
T extends Throwable
>
T

Returns:

the first matching throwable from the throwable chain, null if no match or null input.
throwableOfThrowable
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<T>
the class to search for, subclasses do not match, null returns null.
clazz
,
final int
the (zero-based) index of the starting position, negative treated as zero, larger than chain size returns null.
fromIndex
)

Returns the first Throwable that matches the specified type in the exception chain from a specified index.

public static <
the type of Throwable you are searching.
T extends Throwable
>
T

Returns:

the first matching throwable from the throwable chain, null if no match or null input.
throwableOfType
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<T>
the type to search for, subclasses match, null returns null.
type
)

Returns the throwable of the first Throwable that matches the specified class or subclass in the exception chain.

public static <
the type of Throwable you are searching.
T extends Throwable
>
T

Returns:

the first matching throwable from the throwable chain, null if no match or null input.
throwableOfType
(final Throwable
the throwable to inspect, may be null.
throwable
,
final Class<T>
the type to search for, subclasses match, null returns null.
type
,
final int
the (zero-based) index of the starting position, negative treated as zero, larger than chain size returns null.
fromIndex
)

Returns the first Throwable that matches the specified type in the exception chain from a specified index.

public static <
The Throwable type.
T
>
T

Returns:

the given throwable.
throwUnchecked
(final T
the throwable to test and throw or return.
throwable
)
Deprecated Use throwUnchecked(Throwable).

Tests whether the specified Throwable is unchecked and throws it if so.

public static <
The Throwable type.
T extends Throwable
>
T

Returns:

the given throwable.
throwUnchecked
(final T
the throwable to test and throw or return.
throwable
)

Tests whether the specified Throwable is unchecked and throws it if so.

public static <
The type of the returned value.
R
>
R

Returns:

Never actually returned, this generic type matches any type which the calling site requires. "Returning" the results of this method will satisfy the Java compiler requirement that all code paths return a value.
wrapAndThrow
(final Throwable
The throwable to rethrow.
throwable
)

Throws a checked exception without adding the exception to the throws clause of the calling method.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait