public class ExceptionUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
ExceptionUtils.TransformedException
Exception which is constructed from
makeRuntime(Throwable) when the exception was
not a runtime exception. |
static class |
ExceptionUtils.TransformedSuppressedStackException
Exception which is constructed from
makeRuntime(Throwable, boolean) when the
exception was not a runtime exception, and stack is being suppressed. |
Constructor and Description |
---|
ExceptionUtils() |
Modifier and Type | Method and Description |
---|---|
static <T extends java.lang.Throwable> |
getCauseOfType(java.lang.Throwable rootError,
java.lang.Class<? extends T> type)
Checks to see if the provided error, or any causes in the provided error matching the
provided type.
|
static java.lang.Throwable |
getCauseOfTypes(java.lang.Throwable rootError,
java.lang.Iterable<? extends java.lang.Class<? extends java.lang.Throwable>> types)
Checks to see if the provided error, or any causes in the provided error matching the
provided type.
|
static ExceptionHandler |
getExceptionHandler()
Gets the set
ExceptionHandler if one is set, or null if none is set. |
static java.lang.Throwable |
getRootCause(java.lang.Throwable throwable)
Gets the root cause of a provided
Throwable . |
static ExceptionHandler |
getThreadLocalExceptionHandler()
Gets the thread local
ExceptionHandler if one is set, or null if none
is set. |
static void |
handleException(java.lang.Throwable t)
This call handles an uncaught throwable.
|
static boolean |
hasCauseOfType(java.lang.Throwable rootError,
java.lang.Class<? extends java.lang.Throwable> type)
Checks to see if the provided error, or any causes in the provided error match the provided
type.
|
static boolean |
hasCauseOfTypes(java.lang.Throwable rootError,
java.lang.Iterable<? extends java.lang.Class<? extends java.lang.Throwable>> types)
Checks to see if the provided error, or any causes in the provided error match the provided
type.
|
static java.lang.RuntimeException |
makeRuntime(java.lang.Throwable t)
Makes a r
RuntimeException if necessary. |
static java.lang.RuntimeException |
makeRuntime(java.lang.Throwable t,
boolean suppressWrappedStack)
Makes a r
RuntimeException if necessary. |
static void |
runRunnable(java.lang.Runnable r)
Deprecated.
Please use
SameThreadSubmitterExecutor.instance().execute(r) as an alternative |
static void |
setDefaultExceptionHandler(ExceptionHandler exceptionHandler)
Sets the default
ExceptionHandler to be used by all threads. |
static void |
setInheritableExceptionHandler(ExceptionHandler exceptionHandler)
Sets the
ExceptionHandler for this thread, and any threads that spawn off of this
thread. |
static void |
setThreadExceptionHandler(ExceptionHandler exceptionHandler)
Sets the
ExceptionHandler for this thread. |
static java.lang.String |
stackToString(java.lang.StackTraceElement[] stack)
Writes the stack trace array out to a string.
|
static java.lang.String |
stackToString(java.lang.Throwable t)
Convert throwable's stack and message into a simple string.
|
static void |
writeStackTo(java.lang.StackTraceElement[] stack,
java.lang.StringBuilder stringBuilder)
Writes the stack to the provided StringBuilder.
|
static void |
writeStackTo(java.lang.Throwable t,
java.lang.StringBuffer sb)
Formats and writes a throwable's stack trace to a provided
StringBuffer . |
static void |
writeStackTo(java.lang.Throwable t,
java.lang.StringBuilder sb)
Formats and writes a throwable's stack trace to a provided
StringBuilder . |
static void |
writeStackTo(java.lang.Throwable t,
java.io.Writer w)
Formats and writes a throwable's stack trace to a provided
Writer . |
public static void setThreadExceptionHandler(ExceptionHandler exceptionHandler)
ExceptionHandler
for this thread. This exception handler will be
called if this thread calls to handleException(Throwable)
.exceptionHandler
- Exception handler instance, or null
to remove any handlerpublic static void setInheritableExceptionHandler(ExceptionHandler exceptionHandler)
ExceptionHandler
for this thread, and any threads that spawn off of this
thread. If this thread, or any children threads (that do not override their
ExceptionHandler
), calls handleException(Throwable)
,
the provided interface will be called.exceptionHandler
- Exception handler instance, or null
to remove any handlerpublic static void setDefaultExceptionHandler(ExceptionHandler exceptionHandler)
ExceptionHandler
to be used by all threads. Assuming a threads
local, or inheritable ExceptionHandler
has not been set, this default instance will
be relied on.exceptionHandler
- Exception handler instance, or null
to remove any handlerpublic static ExceptionHandler getThreadLocalExceptionHandler()
ExceptionHandler
if one is set, or null
if none
is set. Since getExceptionHandler()
prioritizes to the thread local handler, this can
be used to get a reference to the current handler before changing the thread local handler to
ensure that getExceptionHandler()
(and down stream use like
handleException(Throwable)
) invoke a handler of your choosing. Once done you can then
choose to reset the original handler with the one returned from this invocation.null
if none is setpublic static ExceptionHandler getExceptionHandler()
ExceptionHandler
if one is set, or null
if none is set.
This prioritizes to the threads locally set handler, with the second priority being an inherited
handler, with the final option being the default handler. If none of those are set, a
null
is returned.null
if none are available@Deprecated public static void runRunnable(java.lang.Runnable r)
SameThreadSubmitterExecutor.instance().execute(r)
as an alternativeRunnable.run()
on the provided runnable on this thread, ensuring that no
throwables are thrown out of this invocation. If any throwable's are thrown, they will be
provided to handleException(Throwable)
.r
- Runnable to invoke, can not be nullpublic static void handleException(java.lang.Throwable t)
t
- throwable to handlepublic static java.lang.RuntimeException makeRuntime(java.lang.Throwable t)
RuntimeException
if necessary. If provided exception is already a
RuntimeException
then it is just directly returned. If it has to produce a new
exception the stack is updated to omit this call.
If the point of wrapping the stack is not useful in debugging consider providing a
true
into. makeRuntime(Throwable, boolean)
.
t
- Throwable
which may or may not be a runtimeExceptionRuntimeException
based on provided Throwable
public static java.lang.RuntimeException makeRuntime(java.lang.Throwable t, boolean suppressWrappedStack)
RuntimeException
if necessary. If provided exception is already a
RuntimeException
then it is just directly returned. If it has to produce a new
exception, you can control if a stack is generated by providing a true
to suppress the
generation (which in java can be fairly expensive). If stack generation is not suppressed (ie
false
is specified), then the stack will be modified to omit this call.t
- Throwable
which may or may not be a runtimeExceptionsuppressWrappedStack
- true
to avoid generating a stack traceRuntimeException
based on provided Throwable
public static java.lang.Throwable getRootCause(java.lang.Throwable throwable)
Throwable
. If there is no cause for the
Throwable
provided into this function, the original Throwable
is returned.
This function does basic circular cause detection in that it will detect a cycle to the
originally provided Throwable
. But other circular chains of causes may result in an
infinite loop.
throwable
- starting Throwable
Throwable
public static java.lang.Throwable getCauseOfTypes(java.lang.Throwable rootError, java.lang.Iterable<? extends java.lang.Class<? extends java.lang.Throwable>> types)
hasCauseOfTypes(Throwable, Iterable)
. If you are only comparing against one exception
type getCauseOfType(Throwable, Class)
is a better option (and will return without the
need to cast, type thanks to generics).
This function does basic circular cause detection in that it will detect a cycle to the
originally provided Throwable
. But other circular chains of causes may result in an
infinite loop.
rootError
- Throwable to start search fromtypes
- Types of throwable classes looking to match againstnull
if none was foundpublic static boolean hasCauseOfTypes(java.lang.Throwable rootError, java.lang.Iterable<? extends java.lang.Class<? extends java.lang.Throwable>> types)
getCauseOfTypes(Throwable, Iterable)
. If you are
only comparing against one exception type hasCauseOfType(Throwable, Class)
is a
better option.rootError
- Throwable to start search fromtypes
- Types of throwable classes looking to match againsttrue
if a match was found, false if no exception cause matches any provided typespublic static <T extends java.lang.Throwable> T getCauseOfType(java.lang.Throwable rootError, java.lang.Class<? extends T> type)
hasCauseOfType(Throwable, Class)
.
This function does basic circular cause detection in that it will detect a cycle to the
originally provided Throwable
. But other circular chains of causes may result in an
infinite loop.
T
- Type of throwable to return (must equal or be super type of generic class provided)rootError
- Throwable to start search fromtype
- Type of throwable classes looking to match againstnull
if none was foundpublic static boolean hasCauseOfType(java.lang.Throwable rootError, java.lang.Class<? extends java.lang.Throwable> type)
getCauseOfType(Throwable, Class)
.rootError
- Throwable to start search fromtype
- Type of throwable classes looking to match againsttrue
if a match was found, false if no exception cause matches any provided typespublic static java.lang.String stackToString(java.lang.Throwable t)
t
- throwable which contains stackpublic static void writeStackTo(java.lang.Throwable t, java.lang.StringBuilder sb)
StringBuilder
.t
- Throwable
which contains stacksb
- StringBuilder to write output topublic static void writeStackTo(java.lang.Throwable t, java.lang.StringBuffer sb)
StringBuffer
.t
- Throwable
which contains stacksb
- StringBuffer to write output topublic static void writeStackTo(java.lang.Throwable t, java.io.Writer w)
Writer
.t
- Throwable
which contains stackw
- Writer to write output topublic static java.lang.String stackToString(java.lang.StackTraceElement[] stack)
stackToString(Throwable)
from a throwable would.stack
- Array of stack elements to build the string off ofpublic static void writeStackTo(java.lang.StackTraceElement[] stack, java.lang.StringBuilder stringBuilder)
writeStackTo(Throwable, StringBuilder)
would.stack
- Array of stack elements to build the string off ofstringBuilder
- StringBuilder to write the stack out to