| Constructor and Description |
|---|
ExceptionUtils()
Deprecated.
TODO Make private in 4.0.
|
| Modifier and Type | Method and Description |
|---|---|
static <T extends RuntimeException> |
asRuntimeException(Throwable throwable)
Throws the given (usually checked) exception without adding the exception to the throws
clause of the calling method.
|
static void |
forEach(Throwable throwable,
Consumer<Throwable> consumer)
Performs an action for each Throwable causes of the given Throwable.
|
static Throwable |
getCause(Throwable throwable)
Deprecated.
This feature will be removed in Lang 4, use
Throwable.getCause() instead |
static Throwable |
getCause(Throwable throwable,
String[] methodNames)
Deprecated.
This feature will be removed in Lang 4, use
Throwable.getCause() instead |
static String[] |
getDefaultCauseMethodNames()
Deprecated.
This feature will be removed in Lang 4
|
static String |
getMessage(Throwable th)
Gets a short message summarizing the exception.
|
static Throwable |
getRootCause(Throwable throwable)
Walks the
Throwable to obtain its root cause. |
static String |
getRootCauseMessage(Throwable throwable)
Gets a short message summarizing the root cause exception.
|
static String[] |
getRootCauseStackTrace(Throwable throwable)
Gets a compact stack trace for the root cause of the supplied
Throwable. |
static List<String> |
getRootCauseStackTraceList(Throwable throwable)
Gets a compact stack trace for the root cause of the supplied
Throwable. |
static String[] |
getStackFrames(Throwable throwable)
Gets the stack trace associated with the specified
Throwable object, decomposing it into a list of
stack frames. |
static String |
getStackTrace(Throwable throwable)
Gets the stack trace from a Throwable as a String, including suppressed and cause exceptions.
|
static int |
getThrowableCount(Throwable throwable)
Gets a count of the number of
Throwable objects in the
exception chain. |
static List<Throwable> |
getThrowableList(Throwable throwable)
Gets the list of
Throwable objects in the
exception chain. |
static Throwable[] |
getThrowables(Throwable throwable)
Gets the list of
Throwable objects in the
exception chain. |
static boolean |
hasCause(Throwable chain,
Class<? extends Throwable> type)
Tests if the throwable's causal chain have an immediate or wrapped exception
of the given type?
|
static int |
indexOfThrowable(Throwable throwable,
Class<? extends Throwable> clazz)
Returns the (zero-based) index of the first
Throwable
that matches the specified class (exactly) in the exception chain. |
static int |
indexOfThrowable(Throwable throwable,
Class<? extends Throwable> clazz,
int fromIndex)
Returns the (zero-based) index of the first
Throwable
that matches the specified type in the exception chain from
a specified index. |
static int |
indexOfType(Throwable throwable,
Class<? extends Throwable> type)
Returns the (zero-based) index of the first
Throwable
that matches the specified class or subclass in the exception chain. |
static int |
indexOfType(Throwable throwable,
Class<? extends Throwable> type,
int fromIndex)
Returns the (zero-based) index of the first
Throwable
that matches the specified type in the exception chain from
a specified index. |
static boolean |
isChecked(Throwable throwable)
Checks if a throwable represents a checked exception
|
static boolean |
isUnchecked(Throwable throwable)
Checks if a throwable represents an unchecked exception
|
static void |
printRootCauseStackTrace(Throwable throwable)
Prints a compact stack trace for the root cause of a throwable
to
System.err. |
static void |
printRootCauseStackTrace(Throwable throwable,
PrintStream printStream)
Prints a compact stack trace for the root cause of a throwable.
|
static void |
printRootCauseStackTrace(Throwable throwable,
PrintWriter printWriter)
Prints a compact stack trace for the root cause of a throwable.
|
static void |
removeCommonFrames(List<String> causeFrames,
List<String> wrapperFrames)
Removes common frames from the cause trace given the two stack traces.
|
static <T> T |
rethrow(Throwable throwable)
Throws the given (usually checked) exception without adding the exception to the throws
clause of the calling method.
|
static Stream<Throwable> |
stream(Throwable throwable)
Streams causes of a Throwable.
|
static <T extends Throwable> |
throwableOfThrowable(Throwable throwable,
Class<T> clazz)
Returns the first
Throwable
that matches the specified class (exactly) in the exception chain. |
static <T extends Throwable> |
throwableOfThrowable(Throwable throwable,
Class<T> clazz,
int fromIndex)
Returns the first
Throwable
that matches the specified type in the exception chain from
a specified index. |
static <T extends Throwable> |
throwableOfType(Throwable throwable,
Class<T> type)
Returns the throwable of the first
Throwable
that matches the specified class or subclass in the exception chain. |
static <T extends Throwable> |
throwableOfType(Throwable throwable,
Class<T> type,
int fromIndex)
Returns the first
Throwable
that matches the specified type in the exception chain from
a specified index. |
static <T> T |
throwUnchecked(T throwable)
Deprecated.
|
static <T extends Throwable> |
throwUnchecked(T throwable)
Tests whether the specified
Throwable is unchecked and throws it if so. |
static <R> R |
wrapAndThrow(Throwable throwable)
Throws a checked exception without adding the exception to the throws
clause of the calling method.
|
@Deprecated public ExceptionUtils()
ExceptionUtils to be created, although that is not
normally necessary.public static <T extends RuntimeException> T asRuntimeException(Throwable throwable)
The use of this technique may be controversial, but useful.
// There is no throws clause in the method signature.
public int propagateExample {
try {
// Throws IOException
invocation();
} catch (Exception e) {
// Propagates a checked exception.
throw ExceptionUtils.asRuntimeException(e);
}
// more processing
...
return value;
}
This is an alternative to the more conservative approach of wrapping the checked exception in a RuntimeException:
// There is no throws clause in the method signature.
public int wrapExample() {
try {
// throws IOException.
invocation();
} catch (Error e) {
throw e;
} catch (RuntimeException e) {
// Throws an unchecked exception.
throw e;
} catch (Exception e) {
// Wraps a checked exception.
throw new UndeclaredThrowableException(e);
}
// more processing
...
return value;
}
One downside to using this approach is that the Java compiler will not
allow invoking code to specify a checked exception in a catch clause
unless there is some code path within the try block that has invoked a
method declared with that checked exception. If the invoking site wishes
to catch the shaded checked exception, it must either invoke the shaded
code through a method re-declaring the desired checked exception, or
catch Exception and use the instanceof operator. Either of these
techniques are required when interacting with non-Java JVM code such as
Jython, Scala, or Groovy, since these languages do not consider any
exceptions as checked.
T - The type of the returned value.throwable - The throwable to rethrow.wrapAndThrow(Throwable)public static void forEach(Throwable throwable, Consumer<Throwable> consumer)
A throwable without cause will return a stream containing one element - the input throwable. A throwable with one cause
will return a stream containing two elements. - the input throwable and the cause throwable. A null throwable
will return a stream of count zero.
This method handles recursive cause structures that might otherwise cause infinite loops. The cause chain is processed until the end is reached, or until the next item in the chain is already in the result set.
throwable - The Throwable to traverse.consumer - a non-interfering action to perform on the elements.@Deprecated public static Throwable getCause(Throwable throwable)
Throwable.getCause() insteadThrowable 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.
The default list searched for are:
getCause()getNextException()getTargetException()getException()getSourceException()getRootCause()getCausedByException()getNested()If none of the above is found, returns null.
throwable - the throwable to introspect for a cause, may be nullThrowable,
null if none found or null throwable input@Deprecated public static Throwable getCause(Throwable throwable, String[] methodNames)
Throwable.getCause() insteadThrowable to obtain the cause.
A null set of method names means use the default set.
A null in the set of method names will be ignored.
throwable - the throwable to introspect for a cause, may be nullmethodNames - the method names, null treated as default setThrowable,
null if none found or null throwable input@Deprecated public static String[] getDefaultCauseMethodNames()
This may be modified and used in the overloaded getCause(Throwable, String[]) method.
public static String getMessage(Throwable th)
The message returned is of the form {ClassNameWithoutPackage}: {ThrowableMessage}
th - the throwable to get a message for, null returns empty stringpublic static Throwable getRootCause(Throwable throwable)
Throwable to obtain its root cause.
This method walks through the exception chain until the last element,
the root cause of the chain, using Throwable.getCause(), and
returns that exception.
This method handles recursive cause chains that might otherwise cause infinite loops. The cause chain is processed until the end, or until the next item in the chain is already processed. If we detect a loop, then return the element before the loop.
throwable - the throwable to get the root cause for, may be nullThrowable,
null if null throwable inputpublic static String getRootCauseMessage(Throwable throwable)
The message returned is of the form {ClassNameWithoutPackage}: {ThrowableMessage}
throwable - the throwable to get a message for, null returns empty stringpublic static String[] getRootCauseStackTrace(Throwable throwable)
Throwable.
The output of this method is consistent across JDK versions. It consists of the root exception followed by each of its wrapping exceptions separated by '[wrapped]'. Note that this is the opposite order to the JDK1.4 display.
throwable - the throwable to examine, may be nullpublic static List<String> getRootCauseStackTraceList(Throwable throwable)
Throwable.
The output of this method is consistent across JDK versions. It consists of the root exception followed by each of its wrapping exceptions separated by '[wrapped]'. Note that this is the opposite order to the JDK1.4 display.
throwable - the throwable to examine, may be nullpublic static String[] getStackFrames(Throwable throwable)
Throwable object, decomposing it into a list of
stack frames.
The result of this method vary by JDK version as this method
uses Throwable.printStackTrace(java.io.PrintWriter).
throwable - the Throwable to examine, may be nullpublic static String getStackTrace(Throwable throwable)
The result of this method vary by JDK version as this method
uses Throwable.printStackTrace(java.io.PrintWriter).
throwable - the Throwable to be examined, may be nullprintStackTrace(PrintWriter) method, or an empty String if null inputpublic static int getThrowableCount(Throwable throwable)
Throwable objects in the
exception chain.
A throwable without cause will return 1.
A throwable with one cause will return 2 and so on.
A null throwable will return 0.
This method handles recursive cause chains that might otherwise cause infinite loops. The cause chain is processed until the end, or until the next item in the chain is already in the result.
throwable - the throwable to inspect, may be nullpublic static List<Throwable> getThrowableList(Throwable throwable)
Throwable objects in the
exception chain.
A throwable without cause will return a list containing
one element - the input throwable.
A throwable with one cause will return a list containing
two elements. - the input throwable and the cause throwable.
A null throwable will return a list of size zero.
This method handles recursive cause chains that might otherwise cause infinite loops. The cause chain is processed until the end, or until the next item in the chain is already in the result list.
throwable - the throwable to inspect, may be nullpublic static Throwable[] getThrowables(Throwable throwable)
Throwable objects in the
exception chain.
A throwable without cause will return an array containing
one element - the input throwable.
A throwable with one cause will return an array containing
two elements. - the input throwable and the cause throwable.
A null throwable will return an array of size zero.
This method handles recursive cause chains that might otherwise cause infinite loops. The cause chain is processed until the end, or until the next item in the chain is already in the result array.
throwable - the throwable to inspect, may be nullgetThrowableList(Throwable)public static boolean hasCause(Throwable chain, Class<? extends Throwable> type)
chain - The root of a Throwable causal chain.type - The exception type to test.wrapAndThrow(Throwable)public static int indexOfThrowable(Throwable throwable, Class<? extends Throwable> clazz)
Throwable
that matches the specified class (exactly) in the exception chain.
Subclasses of the specified class do not match - see
indexOfType(Throwable, Class) for the opposite.
A null throwable returns -1.
A null type returns -1.
No match in the chain returns -1.
throwable - the throwable to inspect, may be nullclazz - the class to search for, subclasses do not match, null returns -1public static int indexOfThrowable(Throwable throwable, Class<? extends Throwable> clazz, int fromIndex)
Throwable
that matches the specified type in the exception chain from
a specified index.
Subclasses of the specified class do not match - see
indexOfType(Throwable, Class, int) for the opposite.
A null throwable returns -1.
A null type returns -1.
No match in the chain returns -1.
A negative start index is treated as zero.
A start index greater than the number of throwables returns -1.
throwable - the throwable to inspect, may be nullclazz - the class to search for, subclasses do not match, null returns -1fromIndex - the (zero-based) index of the starting position,
negative treated as zero, larger than chain size returns -1public static int indexOfType(Throwable throwable, Class<? extends Throwable> type)
Throwable
that matches the specified class or subclass in the exception chain.
Subclasses of the specified class do match - see
indexOfThrowable(Throwable, Class) for the opposite.
A null throwable returns -1.
A null type returns -1.
No match in the chain returns -1.
throwable - the throwable to inspect, may be nulltype - the type to search for, subclasses match, null returns -1public static int indexOfType(Throwable throwable, Class<? extends Throwable> type, int fromIndex)
Throwable
that matches the specified type in the exception chain from
a specified index.
Subclasses of the specified class do match - see
indexOfThrowable(Throwable, Class) for the opposite.
A null throwable returns -1.
A null type returns -1.
No match in the chain returns -1.
A negative start index is treated as zero.
A start index greater than the number of throwables returns -1.
throwable - the throwable to inspect, may be nulltype - the type to search for, subclasses match, null returns -1fromIndex - the (zero-based) index of the starting position,
negative treated as zero, larger than chain size returns -1public static boolean isChecked(Throwable throwable)
throwable - The throwable to check.public static boolean isUnchecked(Throwable throwable)
throwable - The throwable to check.public static void printRootCauseStackTrace(Throwable throwable)
System.err.
The compact stack trace starts with the root cause and prints stack frames up to the place where it was caught and wrapped. Then it prints the wrapped exception and continues with stack frames until the wrapper exception is caught and wrapped again, etc.
The output of this method is consistent across JDK versions.
The method is equivalent to printStackTrace for throwables
that don't have nested causes.
throwable - the throwable to outputpublic static void printRootCauseStackTrace(Throwable throwable, PrintStream printStream)
The compact stack trace starts with the root cause and prints stack frames up to the place where it was caught and wrapped. Then it prints the wrapped exception and continues with stack frames until the wrapper exception is caught and wrapped again, etc.
The output of this method is consistent across JDK versions. Note that this is the opposite order to the JDK1.4 display.
The method is equivalent to printStackTrace for throwables
that don't have nested causes.
throwable - the throwable to output, may be nullprintStream - the stream to output to, may not be nullNullPointerException - if the printStream is nullpublic static void printRootCauseStackTrace(Throwable throwable, PrintWriter printWriter)
The compact stack trace starts with the root cause and prints stack frames up to the place where it was caught and wrapped. Then it prints the wrapped exception and continues with stack frames until the wrapper exception is caught and wrapped again, etc.
The output of this method is consistent across JDK versions. Note that this is the opposite order to the JDK1.4 display.
The method is equivalent to printStackTrace for throwables
that don't have nested causes.
throwable - the throwable to output, may be nullprintWriter - the writer to output to, may not be nullNullPointerException - if the printWriter is nullpublic static void removeCommonFrames(List<String> causeFrames, List<String> wrapperFrames)
causeFrames - stack trace of a cause throwablewrapperFrames - stack trace of a wrapper throwableNullPointerException - if either argument is nullpublic static <T> T rethrow(Throwable throwable)
The use of this technique may be controversial, but useful.
// There is no throws clause in the method signature.
public int propagateExample() {
try {
// throws SomeCheckedException.
return invocation();
} catch (SomeCheckedException e) {
// Propagates a checked exception and compiles to return an int.
return ExceptionUtils.rethrow(e);
}
}
This is an alternative to the more conservative approach of wrapping the checked exception in a RuntimeException:
// There is no throws clause in the method signature.
public int wrapExample() {
try {
// throws IOException.
return invocation();
} catch (Error e) {
throw e;
} catch (RuntimeException e) {
// Throws an unchecked exception.
throw e;
} catch (Exception e) {
// wraps a checked exception.
throw new UndeclaredThrowableException(e);
}
}
One downside to using this approach is that the Java compiler will not
allow invoking code to specify a checked exception in a catch clause
unless there is some code path within the try block that has invoked a
method declared with that checked exception. If the invoking site wishes
to catch the shaded checked exception, it must either invoke the shaded
code through a method re-declaring the desired checked exception, or
catch Exception and use the instanceof operator. Either of these
techniques are required when interacting with non-Java JVM code such as
Jython, Scala, or Groovy, since these languages do not consider any
exceptions as checked.
T - The type of the return value.throwable - The throwable to rethrow.wrapAndThrow(Throwable)public static Stream<Throwable> stream(Throwable throwable)
A throwable without cause will return a stream containing one element - the input throwable. A throwable with one cause
will return a stream containing two elements. - the input throwable and the cause throwable. A null throwable
will return a stream of count zero.
This method handles recursive cause chains that might otherwise cause infinite loops. The cause chain is processed until the end, or until the next item in the chain is already in the result.
throwable - The Throwable to traversepublic static <T extends Throwable> T throwableOfThrowable(Throwable throwable, Class<T> clazz)
Throwable
that matches the specified class (exactly) in the exception chain.
Subclasses of the specified class do not match - see
throwableOfType(Throwable, Class) for the opposite.
A null throwable returns null.
A null type returns null.
No match in the chain returns null.
T - the type of Throwable you are searching.throwable - the throwable to inspect, may be nullclazz - the class to search for, subclasses do not match, null returns nullpublic static <T extends Throwable> T throwableOfThrowable(Throwable throwable, Class<T> clazz, int fromIndex)
Throwable
that matches the specified type in the exception chain from
a specified index.
Subclasses of the specified class do not match - see
throwableOfType(Throwable, Class, int) for the opposite.
A null throwable returns null.
A null type returns null.
No match in the chain returns null.
A negative start index is treated as zero.
A start index greater than the number of throwables returns null.
T - the type of Throwable you are searching.throwable - the throwable to inspect, may be nullclazz - the class to search for, subclasses do not match, null returns nullfromIndex - the (zero-based) index of the starting position,
negative treated as zero, larger than chain size returns nullpublic static <T extends Throwable> T throwableOfType(Throwable throwable, Class<T> type)
Throwable
that matches the specified class or subclass in the exception chain.
Subclasses of the specified class do match - see
throwableOfThrowable(Throwable, Class) for the opposite.
A null throwable returns null.
A null type returns null.
No match in the chain returns null.
T - the type of Throwable you are searching.throwable - the throwable to inspect, may be nulltype - the type to search for, subclasses match, null returns nullpublic static <T extends Throwable> T throwableOfType(Throwable throwable, Class<T> type, int fromIndex)
Throwable
that matches the specified type in the exception chain from
a specified index.
Subclasses of the specified class do match - see
throwableOfThrowable(Throwable, Class) for the opposite.
A null throwable returns null.
A null type returns null.
No match in the chain returns null.
A negative start index is treated as zero.
A start index greater than the number of throwables returns null.
T - the type of Throwable you are searching.throwable - the throwable to inspect, may be nulltype - the type to search for, subclasses match, null returns nullfromIndex - the (zero-based) index of the starting position,
negative treated as zero, larger than chain size returns null@Deprecated public static <T> T throwUnchecked(T throwable)
throwUnchecked(Throwable).Throwable is unchecked and throws it if so.T - The Throwable type.throwable - the throwable to test and throw or return.public static <T extends Throwable> T throwUnchecked(T throwable)
Throwable is unchecked and throws it if so.T - The Throwable type.throwable - the throwable to test and throw or return.public static <R> R wrapAndThrow(Throwable throwable)
The downside to using this approach is that invoking code which needs to handle specific checked exceptions must sniff up the exception chain to determine if the caught exception was caused by the checked exception.
R - The type of the returned value.throwable - The throwable to rethrow.asRuntimeException(Throwable),
hasCause(Throwable, Class)Copyright © 2001–2025 The Apache Software Foundation. All rights reserved.