Class ThrowableUtils
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface used withUtils.safeSupplier(SupplierWithThrowable). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BeanRuntimeExceptionShortcut for creating aBeanRuntimeExceptionwith a message and associated class.static BeanRuntimeExceptionCreates aRuntimeException.static BeanRuntimeExceptionCreates aRuntimeExceptionwrapping the given throwable.static BeanRuntimeExceptionShortcut for creating aBeanRuntimeExceptionwith a cause, message, and associated class.static BeanRuntimeExceptionCreates aRuntimeExceptionwith a cause.static BeanRuntimeExceptionShortcut for creating aBeanRuntimeExceptionwith a cause, message, and associated class.static BeanRuntimeExceptionShortcut for creating aBeanRuntimeExceptionwith a message and associated class.static <T> TcastException(Class<T> type, Throwable t) Casts or wraps the specified throwable to the specified type.static ExecutableExceptionShortcut for creating anExecutableExceptionwith a message.static ExecutableExceptionCreates anExecutableExceptionwrapping the given throwable.static ExecutableExceptionCreates anExecutableExceptionwith a cause.Searches through the cause chain of an exception to find an exception of the specified type.static StringConvenience method for getting a stack trace as a string.static <T extends Throwable>
TgetThrowableCause(Class<T> c, Throwable t) Same asThrowable.getCause()but searches the throwable chain for an exception of the specified type.static intCalculates a 16-bit hash for the specified throwable based on it's stack trace.static IllegalArgumentExceptionillegalArg(String msg, Object... args) Creates anIllegalArgumentException.static IllegalArgumentExceptionillegalArg(Throwable cause) Creates anIllegalArgumentExceptionwrapping the given throwable.static IllegalArgumentExceptionillegalArg(Throwable cause, String msg, Object... args) Creates anIllegalArgumentExceptionwith a cause.static IllegalStateExceptionillegalState(String msg, Object... args) Creates anIllegalStateExceptionwith a formatted message.static IOExceptionCreates anIOException.static IOExceptionCreates anIOExceptionwrapping the given throwable.static IOExceptionCreates anIOExceptionwith a cause.static StringConvenience method for callingThrowable.getLocalizedMessage().static voidprintStackTrace(Throwable t, PrintWriter pw, Integer maxDepth) Prints a stack trace with a maximum depth limit.static voidprintStackTrace(Throwable t, Integer maxDepth) Prints a stack trace with a maximum depth limit to standard error.static RuntimeExceptionCreates aRuntimeException.static RuntimeExceptionCreates aRuntimeExceptionwrapping the given throwable.static RuntimeExceptionCreates aRuntimeExceptionwith a cause.static RuntimeExceptionCreates a newRuntimeException.Creates anUnsupportedOperationExceptionwith a default message.unsupportedOp(String msg, Object... args) Creates anUnsupportedOperationException.unsupportedOp(Throwable cause) Creates anUnsupportedOperationExceptionwrapping the given throwable.unsupportedOp(Throwable cause, String msg, Object... args) Creates anUnsupportedOperationExceptionwith a cause.Creates anUnsupportedOperationExceptionfor read-only objects.
-
Constructor Details
-
ThrowableUtils
public ThrowableUtils()
-
-
Method Details
-
bex
Shortcut for creating aBeanRuntimeExceptionwith a message and associated class.- Parameters:
c- The class associated with the exception.msg- The message.args- OptionalString.format(String, Object...)arguments.- Returns:
- A new
BeanRuntimeException.
-
bex
Shortcut for creating aBeanRuntimeExceptionwith a message and associated class.Same as the
bex(Class, String, Object...)method but accepts aClassInfoinstead of aClass.- Parameters:
c- The class info associated with the exception.msg- The message.args- OptionalString.format(String, Object...)arguments.- Returns:
- A new
BeanRuntimeException.
-
bex
Creates aRuntimeException.- Parameters:
msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new RuntimeException with the formatted message.
-
bex
Creates aRuntimeExceptionwrapping the given throwable.- Parameters:
cause- The cause of the exception.- Returns:
- A new RuntimeException wrapping the given cause.
-
bex
Shortcut for creating aBeanRuntimeExceptionwith a cause, message, and associated class.- Parameters:
e- The cause of the exception.c- The class associated with the exception.msg- The message.args- OptionalString.format(String, Object...)arguments.- Returns:
- A new
BeanRuntimeException.
-
bex
Shortcut for creating aBeanRuntimeExceptionwith a cause, message, and associated class.Same as the
bex(Throwable, Class, String, Object...)method but accepts aClassInfoinstead of aClass.- Parameters:
e- The cause of the exception.c- The class info associated with the exception.msg- The message.args- OptionalString.format(String, Object...)arguments.- Returns:
- A new
BeanRuntimeException.
-
bex
Creates aRuntimeExceptionwith a cause.- Parameters:
cause- The cause of the exception.msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new RuntimeException with the formatted message and cause.
-
exex
Shortcut for creating anExecutableExceptionwith a message.- Parameters:
msg- The message.args- OptionalString.format(String, Object...)arguments.- Returns:
- A new
ExecutableException.
-
exex
Creates anExecutableExceptionwrapping the given throwable.- Parameters:
cause- The cause of the exception.- Returns:
- A new ExecutableException wrapping the given cause.
-
exex
Creates anExecutableExceptionwith a cause.- Parameters:
cause- The cause of the exception.msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new ExecutableException with the formatted message and cause.
-
castException
Casts or wraps the specified throwable to the specified type.- Type Parameters:
T- The class to cast to.- Parameters:
type- The class to cast to.t- The throwable to cast.- Returns:
- Either the same exception if it's already the specified type, or a wrapped exception.
-
findCause
Searches through the cause chain of an exception to find an exception of the specified type.- Type Parameters:
T- The cause type.- Parameters:
e- The exception to search.cause- The cause type to search for.- Returns:
- An
Optionalcontaining the cause if found, or empty if not found.
-
getStackTrace
Convenience method for getting a stack trace as a string.- Parameters:
t- The throwable to get the stack trace from.- Returns:
- The same content that would normally be rendered via
t.printStackTrace()
-
printStackTrace
Prints a stack trace with a maximum depth limit.This method is useful for
StackOverflowErrorsituations where printing the full stack trace can cause additional errors due to stack exhaustion. The stack trace will be limited to the specified maximum number of elements.Example:
try {// Some code that might cause StackOverflowError }catch (StackOverflowErrore ) {// Print only first 100 stack trace elements ThrowableUtils.printStackTrace (e , System.err, 100); }- Parameters:
t- The throwable to print the stack trace for.pw- The print writer to write to.maxDepth- The maximum number of stack trace elements to print. Ifnull or negative, prints all elements.
-
printStackTrace
Prints a stack trace with a maximum depth limit to standard error.This method is useful for
StackOverflowErrorsituations where printing the full stack trace can cause additional errors due to stack exhaustion. The stack trace will be limited to the specified maximum number of elements.Same as
printStackTrace(Throwable, PrintWriter, Integer)but writes toSystem.err.Example:
try {// Some code that might cause StackOverflowError }catch (StackOverflowErrore ) {// Print only first 100 stack trace elements to stderr ThrowableUtils.printStackTrace (e , 100); }- Parameters:
t- The throwable to print the stack trace for.maxDepth- The maximum number of stack trace elements to print. Ifnull or negative, prints all elements.
-
getThrowableCause
Same asThrowable.getCause()but searches the throwable chain for an exception of the specified type.- Type Parameters:
T- The throwable type to search for.- Parameters:
c- The throwable type to search for.t- The throwable to search.- Returns:
- The exception, or
null if not found.
-
hash
Calculates a 16-bit hash for the specified throwable based on it's stack trace.- Parameters:
t- The throwable to calculate the stack trace on.stopClass- Optional stop class on which to stop calculation of a stack trace beyond when found.- Returns:
- A calculated hash.
-
illegalArg
Creates anIllegalArgumentException.- Parameters:
msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new IllegalArgumentException with the formatted message.
-
illegalState
Creates anIllegalStateExceptionwith a formatted message.This is a convenience method for creating state exceptions with formatted messages. The message is formatted using
Utils.f(String, Object...).Example:
throw illegalState ("Invalid state: {0}" ,state );- Parameters:
msg- The message format string.args- The arguments for the message format string.- Returns:
- A new IllegalStateException with the formatted message.
- See Also:
-
illegalArg
Creates anIllegalArgumentExceptionwrapping the given throwable.- Parameters:
cause- The cause of the exception.- Returns:
- A new IllegalArgumentException wrapping the given cause.
-
illegalArg
Creates anIllegalArgumentExceptionwith a cause.- Parameters:
cause- The cause of the exception.msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new IllegalArgumentException with the formatted message and cause.
-
ioex
Creates anIOException.- Parameters:
msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new IOException with the formatted message.
-
ioex
Creates anIOExceptionwrapping the given throwable.- Parameters:
cause- The cause of the exception.- Returns:
- A new IOException wrapping the given cause.
-
ioex
Creates anIOExceptionwith a cause.- Parameters:
cause- The cause of the exception.msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new IOException with the formatted message and cause.
-
lm
Convenience method for callingThrowable.getLocalizedMessage().- Parameters:
t- The throwable.- Returns:
- The localized message of the throwable.
-
rex
Creates aRuntimeException.- Parameters:
msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new RuntimeException with the formatted message.
-
rex
Creates aRuntimeExceptionwrapping the given throwable.- Parameters:
cause- The cause of the exception.- Returns:
- A new RuntimeException wrapping the given cause.
-
rex
Creates aRuntimeExceptionwith a cause.- Parameters:
cause- The cause of the exception.msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new RuntimeException with the formatted message and cause.
-
toRex
Creates a newRuntimeException.- Parameters:
cause- The caused-by exception.- Returns:
- A new
RuntimeException, or the same exception if it's already of that type.
-
unsupportedOp
Creates anUnsupportedOperationExceptionwith a default message.- Returns:
- A new UnsupportedOperationException with the message "Not supported."
-
unsupportedOp
Creates anUnsupportedOperationException.- Parameters:
msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new UnsupportedOperationException with the formatted message.
-
unsupportedOp
Creates anUnsupportedOperationExceptionwrapping the given throwable.- Parameters:
cause- The cause of the exception.- Returns:
- A new UnsupportedOperationException wrapping the given cause.
-
unsupportedOp
public static UnsupportedOperationException unsupportedOp(Throwable cause, String msg, Object... args) Creates anUnsupportedOperationExceptionwith a cause.- Parameters:
cause- The cause of the exception.msg- The exception message.args- The arguments to substitute into the message.- Returns:
- A new UnsupportedOperationException with the formatted message and cause.
-
unsupportedOpReadOnly
Creates anUnsupportedOperationExceptionfor read-only objects.- Returns:
- A new UnsupportedOperationException with the message "Object is read only."
-