Package com.strobel.core
Class ExceptionUtilities
java.lang.Object
com.strobel.core.ExceptionUtilities
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic RuntimeException
static String
static String
static <T extends Throwable>
RuntimeExceptionSneakily rethrows any exception without the compiler complaining if the exception is checked but unhandled.static <T extends Throwable,
R>
REquivalent 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
Rethrows the specified exception only if it is within a narrow subset of 'critical' exceptions, e.g.,ThreadDeath
orVirtualMachineError
.static Throwable
static RuntimeException
-
Constructor Details
-
ExceptionUtilities
public ExceptionUtilities()
-
-
Method Details
-
asRuntimeException
-
unwrap
-
getMessage
-
getStackTraceString
-
rethrowCritical
Rethrows the specified exception only if it is within a narrow subset of 'critical' exceptions, e.g.,ThreadDeath
orVirtualMachineError
. -
rethrow
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
.
-
rethrowAs
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
.
-
wrapOrThrow
-