Package com.palantir.common.base
Class Throwables
- java.lang.Object
-
- com.palantir.common.base.Throwables
-
public final class Throwables extends Object
Utilities for creating and propagating exceptions. Note: Differently from Guava, the methods in this class handle interruption; that is, they will re-set the interrupt flag and throw aPalantirInterruptedException
instead ofPalantirRuntimeException
if incoming exceptions are subclasses ofInterruptedException
.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Throwable>
Tchain(T throwable, Throwable cause)
Simply call throwable.initCause(cause) and return throwable.static String
getThreadDump()
Returns a dump of all threads.static <T extends Throwable>
booleanhasCauseInCausalChain(T throwable, Class<? extends T> causeClass)
Returns true iff an exception of type causeClass exists somewhere in the causal chain.static <T extends Throwable>
Trewrap(String newMessage, T throwable)
Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current thread's stack trace.static <T extends Throwable>
Trewrap(T throwable)
Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current thread's stack trace.static <K extends Throwable>
voidrewrapAndThrowIfInstance(String newMessage, Throwable t, Class<K> clazz)
if (t instanceof K) throw Throwables.rewrap((K)t);static <K extends Throwable>
voidrewrapAndThrowIfInstance(Throwable t, Class<K> clazz)
if (t instanceof K) throw Throwables.rewrap((K)t);static RuntimeException
rewrapAndThrowUncheckedException(String newMessage, Throwable ex)
If Throwable is a RuntimeException or Error, rewrap and throw it.static RuntimeException
rewrapAndThrowUncheckedException(Throwable ex)
If Throwable is a RuntimeException or Error, rewrap and throw it.static RuntimeException
throwCauseAsUnchecked(Exception exception)
static <K extends Throwable>
voidthrowIfInstance(Throwable t, Class<K> clazz)
if (t instanceof K) throw (K)t;static void
throwIfUncheckedException(Throwable ex)
Will simply rethrow the exception if it is aRuntimeException
or anError
static RuntimeException
throwUncheckedException(Throwable ex)
Throws the input Throwable if it is a RuntimeException or Error, otherwise wraps it in a PalantirRuntimeException.static AtlasDbDependencyException
unwrapAndThrowAtlasDbDependencyException(Throwable ex)
If Throwable is a RuntimeException or Error, rethrow it.static Throwable
unwrapIfPossible(Throwable ex)
-
-
-
Method Detail
-
chain
public static <T extends Throwable> T chain(T throwable, Throwable cause)
Simply call throwable.initCause(cause) and return throwable. This makes it easy to chain for old exception types with no chained constructor.throw Throwables.chain(new MyExceptionTypeWithNoChainedConstructor(cause.getMessage()), cause)
instead of
MyExceptionTypeWithNoChainedConstructor throwThis = new MyExceptionTypeWithNoChainedConstructor(cause.getMessage);
throwThis.initCause(cause);
throw throwThis;
-
hasCauseInCausalChain
public static <T extends Throwable> boolean hasCauseInCausalChain(T throwable, Class<? extends T> causeClass)
Returns true iff an exception of type causeClass exists somewhere in the causal chain.
-
rewrapAndThrowUncheckedException
public static RuntimeException rewrapAndThrowUncheckedException(Throwable ex)
If Throwable is a RuntimeException or Error, rewrap and throw it. If not, throw a PalantirRuntimeException.
-
rewrapAndThrowUncheckedException
public static RuntimeException rewrapAndThrowUncheckedException(String newMessage, Throwable ex)
If Throwable is a RuntimeException or Error, rewrap and throw it. If not, throw a PalantirRuntimeException.
-
unwrapAndThrowAtlasDbDependencyException
public static AtlasDbDependencyException unwrapAndThrowAtlasDbDependencyException(Throwable ex)
If Throwable is a RuntimeException or Error, rethrow it. If its an ExecutionException or InvocationTargetException, extract the cause and process it. Else, throw a PalantirRuntimeException.
-
throwUncheckedException
public static RuntimeException throwUncheckedException(Throwable ex)
Throws the input Throwable if it is a RuntimeException or Error, otherwise wraps it in a PalantirRuntimeException.
-
throwIfUncheckedException
public static void throwIfUncheckedException(Throwable ex)
Will simply rethrow the exception if it is aRuntimeException
or anError
-
rewrapAndThrowIfInstance
public static <K extends Throwable> void rewrapAndThrowIfInstance(Throwable t, Class<K> clazz) throws K extends Throwable
if (t instanceof K) throw Throwables.rewrap((K)t);Note: The runtime type of the thrown exception will be the same as t even if clazz is a supertype of t.
- Throws:
K extends Throwable
-
rewrapAndThrowIfInstance
public static <K extends Throwable> void rewrapAndThrowIfInstance(String newMessage, Throwable t, Class<K> clazz) throws K extends Throwable
if (t instanceof K) throw Throwables.rewrap((K)t);Note: The runtime type of the thrown exception will be the same as t even if clazz is a supertype of t.
- Throws:
K extends Throwable
-
throwIfInstance
public static <K extends Throwable> void throwIfInstance(Throwable t, Class<K> clazz) throws K extends Throwable
if (t instanceof K) throw (K)t;Note: The runtime type of the thrown exception will be the same as t even if clazz is a supertype of t.
- Throws:
K extends Throwable
-
rewrap
public static <T extends Throwable> T rewrap(T throwable)
Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current thread's stack trace. Use this method when you are about to rethrow a throwable from another thread, for example when throwingThrowable.getCause()
after callingFuture.get()
;
-
rewrap
public static <T extends Throwable> T rewrap(String newMessage, T throwable)
Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current thread's stack trace. Use this method when you are about to rethrow a throwable from another thread, for example when throwingThrowable.getCause()
after callingFuture.get()
;
-
throwCauseAsUnchecked
public static RuntimeException throwCauseAsUnchecked(Exception exception)
-
getThreadDump
public static String getThreadDump()
Returns a dump of all threads.
-
-