Class ExceptionUtils
- java.lang.Object
-
- org.apache.commons.lang3.exception.ExceptionUtils
-
public class ExceptionUtils extends java.lang.ObjectProvides utilities for manipulating and examiningThrowableobjects.- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description ExceptionUtils()Deprecated.Will be private in 3.0.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T extends java.lang.RuntimeException>
TasRuntimeException(java.lang.Throwable throwable)Use to throws a checked exception without adding the exception to the throws clause of the calling method.static voidforEach(java.lang.Throwable throwable, java.util.function.Consumer<java.lang.Throwable> consumer)Performs an action for each Throwable causes of the given Throwable.static java.lang.ThrowablegetCause(java.lang.Throwable throwable)Deprecated.This feature will be removed in Lang 4, useThrowable.getCause()insteadstatic java.lang.ThrowablegetCause(java.lang.Throwable throwable, java.lang.String[] methodNames)Deprecated.This feature will be removed in Lang 4, useThrowable.getCause()insteadstatic java.lang.String[]getDefaultCauseMethodNames()Deprecated.This feature will be removed in Lang 4static java.lang.StringgetMessage(java.lang.Throwable th)Gets a short message summarizing the exception.static java.lang.ThrowablegetRootCause(java.lang.Throwable throwable)Introspects theThrowableto obtain the root cause.static java.lang.StringgetRootCauseMessage(java.lang.Throwable throwable)Gets a short message summarizing the root cause exception.static java.lang.String[]getRootCauseStackTrace(java.lang.Throwable throwable)Gets a compact stack trace for the root cause of the suppliedThrowable.static java.util.List<java.lang.String>getRootCauseStackTraceList(java.lang.Throwable throwable)Gets a compact stack trace for the root cause of the suppliedThrowable.static java.lang.String[]getStackFrames(java.lang.Throwable throwable)Gets the stack trace associated with the specifiedThrowableobject, decomposing it into a list of stack frames.static java.lang.StringgetStackTrace(java.lang.Throwable throwable)Gets the stack trace from a Throwable as a String.static intgetThrowableCount(java.lang.Throwable throwable)Gets a count of the number ofThrowableobjects in the exception chain.static java.util.List<java.lang.Throwable>getThrowableList(java.lang.Throwable throwable)Gets the list ofThrowableobjects in the exception chain.static java.lang.Throwable[]getThrowables(java.lang.Throwable throwable)Gets the list ofThrowableobjects in the exception chain.static booleanhasCause(java.lang.Throwable chain, java.lang.Class<? extends java.lang.Throwable> type)Tests if the throwable's causal chain have an immediate or wrapped exception of the given type?static intindexOfThrowable(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> clazz)Returns the (zero-based) index of the firstThrowablethat matches the specified class (exactly) in the exception chain.static intindexOfThrowable(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> clazz, int fromIndex)Returns the (zero-based) index of the firstThrowablethat matches the specified type in the exception chain from a specified index.static intindexOfType(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> type)Returns the (zero-based) index of the firstThrowablethat matches the specified class or subclass in the exception chain.static intindexOfType(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> type, int fromIndex)Returns the (zero-based) index of the firstThrowablethat matches the specified type in the exception chain from a specified index.static booleanisChecked(java.lang.Throwable throwable)Checks if a throwable represents a checked exceptionstatic booleanisUnchecked(java.lang.Throwable throwable)Checks if a throwable represents an unchecked exceptionstatic voidprintRootCauseStackTrace(java.lang.Throwable throwable)Prints a compact stack trace for the root cause of a throwable toSystem.err.static voidprintRootCauseStackTrace(java.lang.Throwable throwable, java.io.PrintStream printStream)Prints a compact stack trace for the root cause of a throwable.static voidprintRootCauseStackTrace(java.lang.Throwable throwable, java.io.PrintWriter printWriter)Prints a compact stack trace for the root cause of a throwable.static voidremoveCommonFrames(java.util.List<java.lang.String> causeFrames, java.util.List<java.lang.String> wrapperFrames)Removes common frames from the cause trace given the two stack traces.static <T> Trethrow(java.lang.Throwable throwable)Deprecated.static java.util.stream.Stream<java.lang.Throwable>stream(java.lang.Throwable throwable)Streams causes of a Throwable.static <T extends java.lang.Throwable>
TthrowableOfThrowable(java.lang.Throwable throwable, java.lang.Class<T> clazz)Returns the firstThrowablethat matches the specified class (exactly) in the exception chain.static <T extends java.lang.Throwable>
TthrowableOfThrowable(java.lang.Throwable throwable, java.lang.Class<T> clazz, int fromIndex)Returns the firstThrowablethat matches the specified type in the exception chain from a specified index.static <T extends java.lang.Throwable>
TthrowableOfType(java.lang.Throwable throwable, java.lang.Class<T> type)Returns the throwable of the firstThrowablethat matches the specified class or subclass in the exception chain.static <T extends java.lang.Throwable>
TthrowableOfType(java.lang.Throwable throwable, java.lang.Class<T> type, int fromIndex)Returns the firstThrowablethat matches the specified type in the exception chain from a specified index.static <T> TthrowUnchecked(T throwable)Deprecated.static <R> RwrapAndThrow(java.lang.Throwable throwable)Throws a checked exception without adding the exception to the throws clause of the calling method.
-
-
-
Constructor Detail
-
ExceptionUtils
@Deprecated public ExceptionUtils()
Deprecated.Will be private in 3.0.Public constructor allows an instance ofExceptionUtilsto be created, although that is not normally necessary.
-
-
Method Detail
-
asRuntimeException
public static <T extends java.lang.RuntimeException> T asRuntimeException(java.lang.Throwable throwable)
Use to throws a checked exception without adding the exception to the throws clause of the calling method. This method prevents throws clause pollution and reduces the clutter of "Caused by" exceptions in the stack trace.The use of this technique may be controversial, but exceedingly useful to library developers.
public int propagateExample { // note that there is no throws clause try { return invocation(); // throws IOException } catch (Exception e) { return ExceptionUtils.rethrowRuntimeException(e); // propagates a checked exception } }This is an alternative to the more conservative approach of wrapping the checked exception in a RuntimeException:
public int wrapExample { // note that there is no throws clause try { return invocation(); // throws IOException } catch (Error e) { throw e; } catch (RuntimeException e) { throw e; // wraps a checked exception } catch (Exception e) { throw new UndeclaredThrowableException(e); // wraps a checked exception } }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
instanceofoperator. 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.- Type Parameters:
T- The type of the returned value.- Parameters:
throwable- The throwable to rethrow.- 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.
- Since:
- 3.14.0
- See Also:
wrapAndThrow(Throwable)
-
forEach
public static void forEach(java.lang.Throwable throwable, java.util.function.Consumer<java.lang.Throwable> consumer)Performs an action for each Throwable causes of the given 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
nullthrowable 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.
- Parameters:
throwable- The Throwable to traverse.consumer- a non-interfering action to perform on the elements.- Since:
- 3.13.0
-
getCause
@Deprecated public static java.lang.Throwable getCause(java.lang.Throwable throwable)
Deprecated.This feature will be removed in Lang 4, useThrowable.getCause()insteadIntrospects theThrowableto obtain the cause.The method searches for methods with specific names that return a
Throwableobject. 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.- Parameters:
throwable- the throwable to introspect for a cause, may be null- Returns:
- the cause of the
Throwable,nullif none found or null throwable input - Since:
- 1.0
-
getCause
@Deprecated public static java.lang.Throwable getCause(java.lang.Throwable throwable, java.lang.String[] methodNames)Deprecated.This feature will be removed in Lang 4, useThrowable.getCause()insteadIntrospects theThrowableto obtain the cause.A
nullset of method names means use the default set. Anullin the set of method names will be ignored.- Parameters:
throwable- the throwable to introspect for a cause, may be nullmethodNames- the method names, null treated as default set- Returns:
- the cause of the
Throwable,nullif none found or null throwable input - Since:
- 1.0
-
getDefaultCauseMethodNames
@Deprecated public static java.lang.String[] getDefaultCauseMethodNames()
Deprecated.This feature will be removed in Lang 4Gets the default names used when searching for the cause of an exception.This may be modified and used in the overloaded getCause(Throwable, String[]) method.
- Returns:
- cloned array of the default method names
- Since:
- 3.0
-
getMessage
public static java.lang.String getMessage(java.lang.Throwable th)
Gets a short message summarizing the exception.The message returned is of the form {ClassNameWithoutPackage}: {ThrowableMessage}
- Parameters:
th- the throwable to get a message for, null returns empty string- Returns:
- the message, non-null
- Since:
- 2.2
-
getRootCause
public static java.lang.Throwable getRootCause(java.lang.Throwable throwable)
Introspects theThrowableto obtain the root cause.This method walks through the exception chain to the last element, "root" of the tree, using
Throwable.getCause(), and returns that exception.From version 2.2, this method handles recursive cause structures that might otherwise cause infinite loops. If the throwable parameter has a cause of itself, then null will be returned. If the throwable parameter cause chain loops, the last element in the chain before the loop is returned.
- Parameters:
throwable- the throwable to get the root cause for, may be null- Returns:
- the root cause of the
Throwable,nullif null throwable input
-
getRootCauseMessage
public static java.lang.String getRootCauseMessage(java.lang.Throwable throwable)
Gets a short message summarizing the root cause exception.The message returned is of the form {ClassNameWithoutPackage}: {ThrowableMessage}
- Parameters:
throwable- the throwable to get a message for, null returns empty string- Returns:
- the message, non-null
- Since:
- 2.2
-
getRootCauseStackTrace
public static java.lang.String[] getRootCauseStackTrace(java.lang.Throwable throwable)
Gets a compact stack trace for the root cause of the suppliedThrowable.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.
- Parameters:
throwable- the throwable to examine, may be null- Returns:
- an array of stack trace frames, never null
- Since:
- 2.0
-
getRootCauseStackTraceList
public static java.util.List<java.lang.String> getRootCauseStackTraceList(java.lang.Throwable throwable)
Gets a compact stack trace for the root cause of the suppliedThrowable.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.
- Parameters:
throwable- the throwable to examine, may be null- Returns:
- a list of stack trace frames, never null
- Since:
- 3.13.0
-
getStackFrames
public static java.lang.String[] getStackFrames(java.lang.Throwable throwable)
Gets the stack trace associated with the specifiedThrowableobject, 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). On JDK1.3 and earlier, the cause exception will not be shown unless the specified throwable alters printStackTrace.- Parameters:
throwable- theThrowableto examine, may be null- Returns:
- an array of strings describing each stack frame, never null
-
getStackTrace
public static java.lang.String getStackTrace(java.lang.Throwable throwable)
Gets the stack trace from a Throwable as a String.The result of this method vary by JDK version as this method uses
Throwable.printStackTrace(java.io.PrintWriter). On JDK1.3 and earlier, the cause exception will not be shown unless the specified throwable alters printStackTrace.- Parameters:
throwable- theThrowableto be examined, may be null- Returns:
- the stack trace as generated by the exception's
printStackTrace(PrintWriter)method, or an empty String ifnullinput
-
getThrowableCount
public static int getThrowableCount(java.lang.Throwable throwable)
Gets a count of the number ofThrowableobjects in the exception chain.A throwable without cause will return
1. A throwable with one cause will return2and so on. Anullthrowable will return0.From version 2.2, 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.
- Parameters:
throwable- the throwable to inspect, may be null- Returns:
- the count of throwables, zero if null input
-
getThrowableList
public static java.util.List<java.lang.Throwable> getThrowableList(java.lang.Throwable throwable)
Gets the list ofThrowableobjects 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
nullthrowable will return a list of size 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.
- Parameters:
throwable- the throwable to inspect, may be null- Returns:
- the list of throwables, never null
- Since:
- 2.2
-
getThrowables
public static java.lang.Throwable[] getThrowables(java.lang.Throwable throwable)
Gets the list ofThrowableobjects 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
nullthrowable will return an array of size zero.From version 2.2, 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.
- Parameters:
throwable- the throwable to inspect, may be null- Returns:
- the array of throwables, never null
- See Also:
getThrowableList(Throwable)
-
hasCause
public static boolean hasCause(java.lang.Throwable chain, java.lang.Class<? extends java.lang.Throwable> type)Tests if the throwable's causal chain have an immediate or wrapped exception of the given type?- Parameters:
chain- The root of a Throwable causal chain.type- The exception type to test.- Returns:
- true, if chain is an instance of type or is an UndeclaredThrowableException wrapping a cause.
- Since:
- 3.5
- See Also:
wrapAndThrow(Throwable)
-
indexOfThrowable
public static int indexOfThrowable(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> clazz)Returns the (zero-based) index of the firstThrowablethat matches the specified class (exactly) in the exception chain. Subclasses of the specified class do not match - seeindexOfType(Throwable, Class)for the opposite.A
nullthrowable returns-1. Anulltype returns-1. No match in the chain returns-1.- Parameters:
throwable- the throwable to inspect, may be nullclazz- the class to search for, subclasses do not match, null returns -1- Returns:
- the index into the throwable chain, -1 if no match or null input
-
indexOfThrowable
public static int indexOfThrowable(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> clazz, int fromIndex)Returns the (zero-based) index of the firstThrowablethat matches the specified type in the exception chain from a specified index. Subclasses of the specified class do not match - seeindexOfType(Throwable, Class, int)for the opposite.A
nullthrowable returns-1. Anulltype 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.- Parameters:
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 -1- Returns:
- the index into the throwable chain, -1 if no match or null input
-
indexOfType
public static int indexOfType(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> type)Returns the (zero-based) index of the firstThrowablethat matches the specified class or subclass in the exception chain. Subclasses of the specified class do match - seeindexOfThrowable(Throwable, Class)for the opposite.A
nullthrowable returns-1. Anulltype returns-1. No match in the chain returns-1.- Parameters:
throwable- the throwable to inspect, may be nulltype- the type to search for, subclasses match, null returns -1- Returns:
- the index into the throwable chain, -1 if no match or null input
- Since:
- 2.1
-
indexOfType
public static int indexOfType(java.lang.Throwable throwable, java.lang.Class<? extends java.lang.Throwable> type, int fromIndex)Returns the (zero-based) index of the firstThrowablethat matches the specified type in the exception chain from a specified index. Subclasses of the specified class do match - seeindexOfThrowable(Throwable, Class)for the opposite.A
nullthrowable returns-1. Anulltype 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.- Parameters:
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 -1- Returns:
- the index into the throwable chain, -1 if no match or null input
- Since:
- 2.1
-
isChecked
public static boolean isChecked(java.lang.Throwable throwable)
Checks if a throwable represents a checked exception- Parameters:
throwable- The throwable to check.- Returns:
- True if the given Throwable is a checked exception.
- Since:
- 3.13.0
-
isUnchecked
public static boolean isUnchecked(java.lang.Throwable throwable)
Checks if a throwable represents an unchecked exception- Parameters:
throwable- The throwable to check.- Returns:
- True if the given Throwable is an unchecked exception.
- Since:
- 3.13.0
-
printRootCauseStackTrace
public static void printRootCauseStackTrace(java.lang.Throwable throwable)
Prints a compact stack trace for the root cause of a throwable toSystem.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. Note that this is the opposite order to the JDK1.4 display.
The method is equivalent to
printStackTracefor throwables that don't have nested causes.- Parameters:
throwable- the throwable to output- Since:
- 2.0
-
printRootCauseStackTrace
public static void printRootCauseStackTrace(java.lang.Throwable throwable, java.io.PrintStream printStream)Prints a compact stack trace for the root cause of a throwable.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
printStackTracefor throwables that don't have nested causes.- Parameters:
throwable- the throwable to output, may be nullprintStream- the stream to output to, may not be null- Throws:
java.lang.NullPointerException- if the printStream isnull- Since:
- 2.0
-
printRootCauseStackTrace
public static void printRootCauseStackTrace(java.lang.Throwable throwable, java.io.PrintWriter printWriter)Prints a compact stack trace for the root cause of a throwable.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
printStackTracefor throwables that don't have nested causes.- Parameters:
throwable- the throwable to output, may be nullprintWriter- the writer to output to, may not be null- Throws:
java.lang.NullPointerException- if the printWriter isnull- Since:
- 2.0
-
removeCommonFrames
public static void removeCommonFrames(java.util.List<java.lang.String> causeFrames, java.util.List<java.lang.String> wrapperFrames)Removes common frames from the cause trace given the two stack traces.- Parameters:
causeFrames- stack trace of a cause throwablewrapperFrames- stack trace of a wrapper throwable- Throws:
java.lang.NullPointerException- if either argument is null- Since:
- 2.0
-
rethrow
@Deprecated public static <T> T rethrow(java.lang.Throwable throwable)
Deprecated.Use to throw a checked exception without adding the exception to the throws clause of the calling method. This method prevents throws clause pollution and reduces the clutter of "Caused by" exceptions in the stack trace.The use of this technique may be controversial, but exceedingly useful to library developers.
public int propagateExample { // note that there is no throws clause try { return invocation(); // throws IOException } catch (Exception e) { return ExceptionUtils.rethrow(e); // propagates a checked exception } }This is an alternative to the more conservative approach of wrapping the checked exception in a RuntimeException:
public int wrapExample { // note that there is no throws clause try { return invocation(); // throws IOException } catch (Error e) { throw e; } catch (RuntimeException e) { throw e; // wraps a checked exception } catch (Exception e) { throw new UndeclaredThrowableException(e); // wraps a checked exception } }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
instanceofoperator. 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.- Type Parameters:
T- The type of the returned value.- Parameters:
throwable- The throwable to rethrow.- 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.
- Since:
- 3.5
- See Also:
wrapAndThrow(Throwable)
-
stream
public static java.util.stream.Stream<java.lang.Throwable> stream(java.lang.Throwable throwable)
Streams causes of a 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
nullthrowable 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.
- Parameters:
throwable- The Throwable to traverse- Returns:
- A new Stream of Throwable causes.
- Since:
- 3.13.0
-
throwableOfThrowable
public static <T extends java.lang.Throwable> T throwableOfThrowable(java.lang.Throwable throwable, java.lang.Class<T> clazz)Returns the firstThrowablethat matches the specified class (exactly) in the exception chain. Subclasses of the specified class do not match - seethrowableOfType(Throwable, Class)for the opposite.A
nullthrowable returnsnull. Anulltype returnsnull. No match in the chain returnsnull.- Type Parameters:
T- the type of Throwable you are searching.- Parameters:
throwable- the throwable to inspect, may be nullclazz- the class to search for, subclasses do not match, null returns null- Returns:
- the first matching throwable from the throwable chain, null if no match or null input
- Since:
- 3.10
-
throwableOfThrowable
public static <T extends java.lang.Throwable> T throwableOfThrowable(java.lang.Throwable throwable, java.lang.Class<T> clazz, int fromIndex)Returns the firstThrowablethat matches the specified type in the exception chain from a specified index. Subclasses of the specified class do not match - seethrowableOfType(Throwable, Class, int)for the opposite.A
nullthrowable returnsnull. Anulltype returnsnull. No match in the chain returnsnull. A negative start index is treated as zero. A start index greater than the number of throwables returnsnull.- Type Parameters:
T- the type of Throwable you are searching.- Parameters:
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 null- Returns:
- the first matching throwable from the throwable chain, null if no match or null input
- Since:
- 3.10
-
throwableOfType
public static <T extends java.lang.Throwable> T throwableOfType(java.lang.Throwable throwable, java.lang.Class<T> type)Returns the throwable of the firstThrowablethat matches the specified class or subclass in the exception chain. Subclasses of the specified class do match - seethrowableOfThrowable(Throwable, Class)for the opposite.A
nullthrowable returnsnull. Anulltype returnsnull. No match in the chain returnsnull.- Type Parameters:
T- the type of Throwable you are searching.- Parameters:
throwable- the throwable to inspect, may be nulltype- the type to search for, subclasses match, null returns null- Returns:
- the first matching throwable from the throwable chain, null if no match or null input
- Since:
- 3.10
-
throwableOfType
public static <T extends java.lang.Throwable> T throwableOfType(java.lang.Throwable throwable, java.lang.Class<T> type, int fromIndex)Returns the firstThrowablethat matches the specified type in the exception chain from a specified index. Subclasses of the specified class do match - seethrowableOfThrowable(Throwable, Class)for the opposite.A
nullthrowable returnsnull. Anulltype returnsnull. No match in the chain returnsnull. A negative start index is treated as zero. A start index greater than the number of throwables returnsnull.- Type Parameters:
T- the type of Throwable you are searching.- Parameters:
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- Returns:
- the first matching throwable from the throwable chain, null if no match or null input
- Since:
- 3.10
-
throwUnchecked
@Deprecated public static <T> T throwUnchecked(T throwable)
Deprecated.Tests whether the specifiedThrowableis unchecked and throws it if so.- Type Parameters:
T- The Throwable type.- Parameters:
throwable- the throwable to test and throw or return.- Returns:
- the given throwable.
- Since:
- 3.13.0
-
throwUnchecked
public static <T extends java.lang.Throwable> T throwUnchecked(T throwable)
Tests whether the specifiedThrowableis unchecked and throws it if so.- Type Parameters:
T- The Throwable type.- Parameters:
throwable- the throwable to test and throw or return.- Returns:
- the given throwable.
- Since:
- 3.14.0
-
wrapAndThrow
public static <R> R wrapAndThrow(java.lang.Throwable throwable)
Throws a checked exception without adding the exception to the throws clause of the calling method. For checked exceptions, this method throws an UndeclaredThrowableException wrapping the checked exception. For Errors and RuntimeExceptions, the original exception is rethrown.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.
- Type Parameters:
R- The type of the returned value.- Parameters:
throwable- The throwable to rethrow.- 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.
- Since:
- 3.5
- See Also:
asRuntimeException(Throwable),hasCause(Throwable, Class)
-
-