Class ThrowableUtils
Exception factory methods (terse rex/brex/iaex/isex/uoex/
uoroex/ioex/exex constructors) live on Shorts
and construct their exceptions directly with no side-effects. This class retains only throwable
inspection/handling helpers (stack-trace rendering, cause walking, safe-run wrappers, casting).
The opt-in log(Throwable) helper checks the juneau.enableVerboseExceptions setting and, when
enabled, prints the throwable's stack trace to the logger. It is not invoked automatically by any
factory — callers that want verbose logging at a specific site must call it explicitly.
Example — enabling for a single test thread:
Settings.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface used withsafeSupplier(SupplierWithThrowable). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TcastException(Class<T> type, Throwable t) Casts or wraps the specified throwable to the specified type.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 StringConvenience method for callingThrowable.getLocalizedMessage().static <T extends Throwable>
Tlog(T exception) Opt-in verbose logger for a throwable.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 voidrunQuietly(Snippet snippet) Runs a snippet, silently ignoring any exception.static voidRuns a snippet; wraps any checked throwable inRuntimeException.static voidsafe(Snippet snippet, Function<Throwable, RuntimeException> exceptionMapper) Runs a snippet; maps any checked throwable via the provided function.static <T> Tsafe(ThrowingSupplier<T> s) Executes a supplier; wraps any checked exception inRuntimeException.static <T> Tsafe(ThrowingSupplier<T> s, Function<Throwable, RuntimeException> exceptionMapper) Executes a supplier; maps any checked exception via the provided function.static <T> TsafeCatch(ThrowingSupplier<T> s, Function<Throwable, T> exceptionFunction) Executes a supplier; returns the result or a fallback value produced by the exception function.static <T> Optional<T>safeOpt(ThrowingSupplier<T> s) Executes a supplier; returns anOptionalof the result, or empty if an exception is thrown.static <T> Optional<T>safeOptCatch(ThrowingSupplier<T> s, Function<Throwable, T> exceptionFunction) Executes a supplier; returns anOptionalof the result or the exception function's value.static <T> TsafeOrNull(ThrowingSupplier<T> s) Executes a supplier; returns the result ornullif an exception is thrown.static <T> TsafeSupplier(ThrowableUtils.SupplierWithThrowable<T> supplier) Executes aThrowableUtils.SupplierWithThrowable; wraps any checked throwable inRuntimeException.static <T> TsafeSupplier(ThrowableUtils.SupplierWithThrowable<T> supplier, Function<Throwable, RuntimeException> exceptionMapper) Executes aThrowableUtils.SupplierWithThrowable; maps any checked throwable via the provided function.static RuntimeExceptionCreates a newRuntimeExceptionfrom the specified cause (or returns it if already one).static RuntimeExceptiontoRuntimeException(Throwable cause) Wraps or casts to RuntimeException.
-
Constructor Details
-
ThrowableUtils
public ThrowableUtils()
-
-
Method Details
-
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. Must not benull .t- The throwable to cast. Can benull (produces a wrapper with anull cause).- 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. -
getStackTrace
Convenience method for getting a stack trace as a string.- Parameters:
t- The throwable to get the stack trace from. Must not benull .- 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. Must not benull .pw- The print writer to write to. Must not benull .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. Must not benull .t- The throwable to search. Can benull (returnsnull ).- 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. Can benull (returns0 ).stopClass- Optional stop class on which to stop calculation of a stack trace beyond when found. Can benull (the entire stack trace, including causes, is used).- Returns:
- A calculated hash.
-
log
Opt-in verbose logger for a throwable.When the
juneau.enableVerboseExceptionssetting istrue, logs the throwable's stack trace as a warning; otherwise does nothing. Returns the same throwable to allow inline use (e.g.throw log(iaex("..."))). This is never invoked automatically — call it explicitly at sites where verbose logging is desired.- Type Parameters:
T- The throwable type.- Parameters:
exception- The throwable to (optionally) log. Must not benull when verbose exceptions are enabled; otherwise returned unchanged (may benull ).- Returns:
- The same throwable.
-
localizedMessage
Convenience method for callingThrowable.getLocalizedMessage().Long messages (>2000 characters) are truncated to prevent stack overflow issues when exception messages contain circular references. The format is: "first-1000-chars<truncated-#-chars>last-1000-chars"
- Parameters:
t- The throwable. Must not benull .- Returns:
- The localized message of the throwable, truncated if necessary, or
null if the throwable has no localized message.
-
toRuntimeException
-
toRex
Creates a newRuntimeExceptionfrom the specified cause (or returns it if already one).- Parameters:
cause- The caused-by exception. Can benull (produces aRuntimeExceptionwith anull cause).- Returns:
- A new
RuntimeException, or the same exception if it's already of that type.
-
safe
Runs a snippet; wraps any checked throwable inRuntimeException. -
safe
Runs a snippet; maps any checked throwable via the provided function. -
safe
Executes a supplier; wraps any checked exception inRuntimeException. -
safe
public static <T> T safe(ThrowingSupplier<T> s, Function<Throwable, RuntimeException> exceptionMapper) Executes a supplier; maps any checked exception via the provided function. -
runQuietly
Runs a snippet, silently ignoring any exception. -
safeCatch
Executes a supplier; returns the result or a fallback value produced by the exception function. -
safeOpt
Executes a supplier; returns anOptionalof the result, or empty if an exception is thrown. -
safeOptCatch
public static <T> Optional<T> safeOptCatch(ThrowingSupplier<T> s, Function<Throwable, T> exceptionFunction) Executes a supplier; returns anOptionalof the result or the exception function's value. -
safeOrNull
Executes a supplier; returns the result ornullif an exception is thrown. -
safeSupplier
Executes aThrowableUtils.SupplierWithThrowable; wraps any checked throwable inRuntimeException. -
safeSupplier
public static <T> T safeSupplier(ThrowableUtils.SupplierWithThrowable<T> supplier, Function<Throwable, RuntimeException> exceptionMapper) Executes aThrowableUtils.SupplierWithThrowable; maps any checked throwable via the provided function.
-