Package org.threadly.util
Class ExceptionUtils
- java.lang.Object
-
- org.threadly.util.ExceptionUtils
-
public class ExceptionUtils extends java.lang.ObjectUtilities for doing basic operations with exceptions.- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classExceptionUtils.TransformedExceptionException which is constructed frommakeRuntime(Throwable)when the exception was not a runtime exception.static classExceptionUtils.TransformedSuppressedStackExceptionException which is constructed frommakeRuntime(Throwable, boolean)when the exception was not a runtime exception, and stack is being suppressed.
-
Constructor Summary
Constructors Constructor Description ExceptionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidchangeStackOverflowCheckFrequency(int frequencyMillis)Update how often the check for StackOverflowErrors being produced inhandleException(Throwable).static <T extends java.lang.Throwable>
TgetCauseOfType(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.ThrowablegetCauseOfTypes(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 ExceptionHandlergetExceptionHandler()Gets the setExceptionHandlerif one is set, ornullif none is set.static java.lang.ThrowablegetRootCause(java.lang.Throwable throwable)Gets the root cause of a providedThrowable.static ExceptionHandlergetThreadLocalExceptionHandler()Gets the thread localExceptionHandlerif one is set, ornullif none is set.static voidhandleException(java.lang.Throwable t)This call handles an uncaught throwable.static booleanhasCauseOfType(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 booleanhasCauseOfTypes(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.RuntimeExceptionmakeRuntime(java.lang.Throwable t)Makes a rRuntimeExceptionif necessary.static java.lang.RuntimeExceptionmakeRuntime(java.lang.Throwable t, boolean suppressWrappedStack)Makes a rRuntimeExceptionif necessary.static voidsetDefaultExceptionHandler(ExceptionHandler exceptionHandler)Sets the defaultExceptionHandlerto be used by all threads.static voidsetInheritableExceptionHandler(ExceptionHandler exceptionHandler)Sets theExceptionHandlerfor this thread, and any threads that spawn off of this thread.static voidsetThreadExceptionHandler(ExceptionHandler exceptionHandler)Sets theExceptionHandlerfor this thread.static java.lang.StringstackToString(java.lang.StackTraceElement[] stack)Writes the stack trace array out to a string.static java.lang.StringstackToString(java.lang.Throwable t)Convert throwable's stack and message into a simple string.static voidwriteStackTo(java.lang.StackTraceElement[] stack, java.lang.StringBuilder stringBuilder)Writes the stack to the provided StringBuilder.static voidwriteStackTo(java.lang.Throwable t, java.io.Writer w)Formats and writes a throwable's stack trace to a providedWriter.static voidwriteStackTo(java.lang.Throwable t, java.lang.StringBuffer sb)Formats and writes a throwable's stack trace to a providedStringBuffer.static voidwriteStackTo(java.lang.Throwable t, java.lang.StringBuilder sb)Formats and writes a throwable's stack trace to a providedStringBuilder.
-
-
-
Method Detail
-
changeStackOverflowCheckFrequency
public static void changeStackOverflowCheckFrequency(int frequencyMillis)
Update how often the check for StackOverflowErrors being produced inhandleException(Throwable). It is important thathandleException(Throwable)never throws an exception, however due to the nature ofStackOverflowErrorthere may be issues in properly reporting the issue. Instead they will be stored internally with a best effort reporting to indicate that they have been occurring (but only one instance per frequency will be reported). By default they are checked every 10 seconds to see if any were thrown. If there was anyStackOverflowErrorthey will be provided back tohandleException(Throwable)on an async thread (as to provide a fresh / shortened stack).- Parameters:
frequencyMillis- Milliseconds between checks for overflow, or negative to disable checks
-
setThreadExceptionHandler
public static void setThreadExceptionHandler(ExceptionHandler exceptionHandler)
Sets theExceptionHandlerfor this thread. This exception handler will be called if this thread calls tohandleException(Throwable).- Parameters:
exceptionHandler- Exception handler instance, ornullto remove any handler
-
setInheritableExceptionHandler
public static void setInheritableExceptionHandler(ExceptionHandler exceptionHandler)
Sets theExceptionHandlerfor this thread, and any threads that spawn off of this thread. If this thread, or any children threads (that do not override theirExceptionHandler), callshandleException(Throwable), the provided interface will be called.- Parameters:
exceptionHandler- Exception handler instance, ornullto remove any handler
-
setDefaultExceptionHandler
public static void setDefaultExceptionHandler(ExceptionHandler exceptionHandler)
Sets the defaultExceptionHandlerto be used by all threads. Assuming a threads local, or inheritableExceptionHandlerhas not been set, this default instance will be relied on.- Parameters:
exceptionHandler- Exception handler instance, ornullto remove any handler
-
getThreadLocalExceptionHandler
public static ExceptionHandler getThreadLocalExceptionHandler()
Gets the thread localExceptionHandlerif one is set, ornullif none is set. SincegetExceptionHandler()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 thatgetExceptionHandler()(and down stream use likehandleException(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.- Returns:
- Thread local ExceptionHandler, or
nullif none is set
-
getExceptionHandler
public static ExceptionHandler getExceptionHandler()
Gets the setExceptionHandlerif one is set, ornullif 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, anullis returned.- Returns:
- Handling instance for this thread, or
nullif none are available
-
handleException
public static void handleException(java.lang.Throwable t)
This call handles an uncaught throwable. If a default uncaught exception handler is set, then that will be called to handle the uncaught exception. If none is set, then the exception will be printed out to standard error.- Parameters:
t- throwable to handle
-
makeRuntime
public static java.lang.RuntimeException makeRuntime(java.lang.Throwable t)
Makes a rRuntimeExceptionif necessary. If provided exception is already aRuntimeExceptionthen 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
trueinto.makeRuntime(Throwable, boolean).- Parameters:
t-Throwablewhich may or may not be a runtimeException- Returns:
- a
RuntimeExceptionbased on providedThrowable
-
makeRuntime
public static java.lang.RuntimeException makeRuntime(java.lang.Throwable t, boolean suppressWrappedStack)Makes a rRuntimeExceptionif necessary. If provided exception is already aRuntimeExceptionthen it is just directly returned. If it has to produce a new exception, you can control if a stack is generated by providing atrueto suppress the generation (which in java can be fairly expensive). If stack generation is not suppressed (iefalseis specified), then the stack will be modified to omit this call.- Parameters:
t-Throwablewhich may or may not be a runtimeExceptionsuppressWrappedStack-trueto avoid generating a stack trace- Returns:
- A
RuntimeExceptionbased on providedThrowable
-
getRootCause
public static java.lang.Throwable getRootCause(java.lang.Throwable throwable)
Gets the root cause of a providedThrowable. If there is no cause for theThrowableprovided into this function, the originalThrowableis returned.If a cyclic exception chain is detected this function will return the cause where the cycle end, and thus the returned Throwable will have a cause associated to it (specifically a cause which is one that was seen earlier in the chain). If no cycle exists this will return a Throwable which contains no cause.
- Parameters:
throwable- startingThrowable- Returns:
- root cause
Throwable
-
getCauseOfTypes
public 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. This can be useful when trying to truncate an exception chain to only the relevant information. If the goal is only to determine if it exists or not consider usinghasCauseOfTypes(Throwable, Iterable). If you are only comparing against one exception typegetCauseOfType(Throwable, Class)is a better option (and will return without the need to cast, type thanks to generics).- Parameters:
rootError- Throwable to start search fromtypes- Types of throwable classes looking to match against- Returns:
- Throwable that matches one of the provided types, or
nullif none was found
-
hasCauseOfTypes
public 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. This can be useful when trying to detect conditions where the actual condition may not be the head cause, nor the root cause (but buried somewhere in the chain). If the actual exception is needed consider usinggetCauseOfTypes(Throwable, Iterable). If you are only comparing against one exception typehasCauseOfType(Throwable, Class)is a better option.- Parameters:
rootError- Throwable to start search fromtypes- Types of throwable classes looking to match against- Returns:
trueif a match was found, false if no exception cause matches any provided types
-
getCauseOfType
public static <T extends java.lang.Throwable> T 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. This can be useful when trying to truncate an exception chain to only the relevant information. If the goal is only to determine if it exists or not consider usinghasCauseOfType(Throwable, Class).- Type Parameters:
T- Type of throwable to return (must equal or be super type of generic class provided)- Parameters:
rootError- Throwable to start search fromtype- Type of throwable classes looking to match against- Returns:
- Throwable that matches one of the provided types, or
nullif none was found
-
hasCauseOfType
public 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. This can be useful when trying to detect conditions where the actual condition may not be the head cause, nor the root cause (but buried somewhere in the chain). If the actual exception is needed consider usinggetCauseOfType(Throwable, Class).- Parameters:
rootError- Throwable to start search fromtype- Type of throwable classes looking to match against- Returns:
trueif a match was found, false if no exception cause matches any provided types
-
stackToString
public static java.lang.String stackToString(java.lang.Throwable t)
Convert throwable's stack and message into a simple string.- Parameters:
t- throwable which contains stack- Returns:
- string which contains the throwable stack trace
-
writeStackTo
public static void writeStackTo(java.lang.Throwable t, java.lang.StringBuilder sb)Formats and writes a throwable's stack trace to a providedStringBuilder.- Parameters:
t-Throwablewhich contains stacksb- StringBuilder to write output to
-
writeStackTo
public static void writeStackTo(java.lang.Throwable t, java.lang.StringBuffer sb)Formats and writes a throwable's stack trace to a providedStringBuffer.- Parameters:
t-Throwablewhich contains stacksb- StringBuffer to write output to
-
writeStackTo
public static void writeStackTo(java.lang.Throwable t, java.io.Writer w)Formats and writes a throwable's stack trace to a providedWriter.- Parameters:
t-Throwablewhich contains stackw- Writer to write output to
-
stackToString
public static java.lang.String stackToString(java.lang.StackTraceElement[] stack)
Writes the stack trace array out to a string. This produces a stack trace string in a very similar way as thestackToString(Throwable)from a throwable would.- Parameters:
stack- Array of stack elements to build the string off of- Returns:
- String which is the stack in a human readable format
-
writeStackTo
public static void writeStackTo(java.lang.StackTraceElement[] stack, java.lang.StringBuilder stringBuilder)Writes the stack to the provided StringBuilder. This produces a stack trace string in a very similar way as thewriteStackTo(Throwable, StringBuilder)would.- Parameters:
stack- Array of stack elements to build the string off ofstringBuilder- StringBuilder to write the stack out to
-
-