Package com.strobel.core
Class ExceptionUtilities
- java.lang.Object
-
- com.strobel.core.ExceptionUtilities
-
public final class ExceptionUtilities extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description ExceptionUtilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.RuntimeException
asRuntimeException(java.lang.Throwable t)
static java.lang.String
getMessage(java.lang.Throwable t)
static java.lang.String
getStackTraceString(java.lang.Throwable t)
static <T extends java.lang.Throwable>
java.lang.RuntimeExceptionrethrow(java.lang.Throwable t)
Sneakily rethrows any exception without the compiler complaining if the exception is checked but unhandled.static <T extends java.lang.Throwable,R>
RrethrowAs(java.lang.Throwable t)
Equivalent torethrow
, but with an open-ended return type, allowing calls to this method to be used as the body of lambda expressions that must return a specific type.static void
rethrowCritical(java.lang.Throwable t)
Rethrows the specified exception only if it is within a narrow subset of 'critical' exceptions, e.g.,ThreadDeath
orVirtualMachineError
.static java.lang.Throwable
unwrap(java.lang.Throwable t)
static java.lang.RuntimeException
wrapOrThrow(java.lang.Throwable t)
-
-
-
Method Detail
-
asRuntimeException
public static java.lang.RuntimeException asRuntimeException(java.lang.Throwable t)
-
unwrap
public static java.lang.Throwable unwrap(java.lang.Throwable t)
-
getMessage
public static java.lang.String getMessage(java.lang.Throwable t)
-
getStackTraceString
public static java.lang.String getStackTraceString(java.lang.Throwable t)
-
rethrowCritical
public static void rethrowCritical(java.lang.Throwable t)
Rethrows the specified exception only if it is within a narrow subset of 'critical' exceptions, e.g.,ThreadDeath
orVirtualMachineError
.
-
rethrow
public static <T extends java.lang.Throwable> java.lang.RuntimeException rethrow(java.lang.Throwable t) throws T extends java.lang.Throwable
Sneakily rethrows any exception without the compiler complaining if the exception is checked but unhandled. The signature declares a return type of
RuntimeException
, for cases where the caller method must exit to satisfy control flow requirements (e.g., final variable assignment), but this method never actually returns a value.Trivial example:
void doSomething() { try { mightThrowCheckedException(); } catch (final Throwable t) { throw ExceptionUtilities.rethrow(t); } }
Example requiring a return value:
T returnSomething() { try { return mightThrowCheckedException(); } catch (final Throwable t) { // The call below always throws, but the compiler doesn't know that and demands // we either return a value or throw. The return value, while never used, allows // us to satisfying the compiler by exiting the current method exceptionally. throw ExceptionUtilities.rethrow(t); } }
Example with constructor and final fields:
class U { final T mustBeAssigned; U() { try { mustBeAssigned = mightThrowCheckedException(); } catch (final Throwable t) { // The compiler requires us to definitively assign all final fields before // returning or throw. Since the compiler doesn't know that the call below // always throws, we can throw the dummy result to satisfy the compiler. throw ExceptionUtilities.rethrow(t); } } }
- Returns:
- This method will never return a value; it always throws.
- Throws:
T
- This method rethrows the original exceptiont
, or aNullPointerException
ift
isnull
.T extends java.lang.Throwable
-
rethrowAs
public static <T extends java.lang.Throwable,R> R rethrowAs(java.lang.Throwable t) throws T extends java.lang.Throwable
Equivalent to
rethrow
, but with an open-ended return type, allowing calls to this method to be used as the body of lambda expressions that must return a specific type.Example:
public static <T, R> Function<T, R> throwing(final Throwable t) { return _ -> ExceptionUtilities.rethrowAs(t); }
- Returns:
- This method will never return a value; it always throws.
- Throws:
T
- This method rethrows the original exceptiont
, or aNullPointerException
ift
isnull
.T extends java.lang.Throwable
-
wrapOrThrow
public static java.lang.RuntimeException wrapOrThrow(java.lang.Throwable t)
-
-