Class ThrowableUtils
java.lang.Object
org.apache.juneau.common.internal.ThrowableUtils
Various utility methods for creating and working with throwables.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Interface used withsafeSupplier(SupplierWithThrowable)
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic RuntimeException
asRuntimeException
(Throwable cause) Creates a newRuntimeException
.static <T> T
Casts or wraps the specified throwable to the specified type.static <T extends Throwable>
TSame asThrowable.getCause()
but searches the throwable chain for an exception of the specified type.static String
Convenience method for getting a stack trace as a string.static int
Calculates a 16-bit hash for the specified throwable based on it's stack trace.static void
Runs a snippet of code and encapsulates any throwable inside aRuntimeException
.static <T> T
safeSupplier
(ThrowableUtils.SupplierWithThrowable<T> supplier) Allows you to wrap a supplier that throws an exception so that it can be used in a fluent interface.
-
Constructor Details
-
ThrowableUtils
public ThrowableUtils()
-
-
Method Details
-
asRuntimeException
Creates a newRuntimeException
.- Parameters:
cause
- The caused-by exception.- Returns:
- A new
RuntimeException
, or the same exception if it's already of that type.
-
cast
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.
-
getCause
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.
-
safeSupplier
Allows you to wrap a supplier that throws an exception so that it can be used in a fluent interface.- Type Parameters:
T
- The supplier type.- Parameters:
supplier
- The supplier throwing an exception.- Returns:
- The supplied result.
- Throws:
RuntimeException
- if supplier threw an exception.
-
safeRun
Runs a snippet of code and encapsulates any throwable inside aRuntimeException
.- Parameters:
snippet
- The snippet of code to run.
-
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()
-
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.
-