Throwable.
See the Guava User Guide entry on Throwables.
| Modifier and Type | Field and Description |
|---|---|
| private static final Method | getStackTraceDepthMethod
The "getStackTraceDepth" method, only available on some JDKs so we use reflection to find it when available. |
| private static final Method | getStackTraceElementMethod
The "getStackTraceElementMethod" method, only available on some JDKs so we use reflection to find it when available. |
| private static final String | JAVA_LANG_ACCESS_CLASSNAME
JavaLangAccess class name to load using reflection |
| private static final Object | jla
Access to some fancy internal JVM internals. |
| pack-priv static final String | SHARED_SECRETS_CLASSNAME
SharedSecrets class name to load using reflection |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static List | Returns: an unmodifiable list containing the cause chain starting withthrowablethe non-null throwable)Throwable to extract causes fromGets a |
| public static <X extends Throwable> X | getCauseAs(Throwable throwable, Class<X> expectedCauseType)
Returns |
| private static Method | getGetMethod()
Returns the Method that can be used to resolve an individual StackTraceElement, or null if that method cannot be found (it is only to be found in fairly recent JDKs). |
| private static Object | |
| private static Method | |
| public static Throwable | |
| private static Method | getSizeMethod(Object jla)
Returns the Method that can be used to return the size of a stack, or null if that method cannot be found (it is only to be found in fairly recent JDKs). |
| public static String | getStackTraceAsString(Throwable throwable)
Returns a string containing the result of |
| private static Object | |
| private static List | |
| public static List | lazyStackTrace(Throwable throwable)
Deprecated
This method is equivalent to
Returns the stack trace of Throwable#getStackTrace() on JDK versions past
JDK 8 and on all Android versions.
throwable, possibly providing slower iteration over the full
trace but faster iteration over parts of the trace.
|
| public static boolean | lazyStackTraceIsLazy()
Deprecated
This method always returns false on JDK versions past JDK 8 and on all Android
versions.
Returns whether lazyStackTrace will use the special implementation described in its
documentation.
|
| public static RuntimeException | Returns: nothing will ever be returned; this return type is only for your convenience, as illustrated in the example abovethe Throwable to propagate throwable)
Deprecated
Use
Propagates throw e or throw new RuntimeException(e) directly, or use a
combination of throwIfUnchecked and throw new RuntimeException(e).
throwable as-is if it is an instance of RuntimeException or Error, or else as a last resort, wraps it in a RuntimeException and then propagates.
|
| public static <X extends Throwable> void | propagateIfInstanceOf(Throwable throwable, Class<X> declaredType)
Deprecated
Use
Propagates throwIfInstanceOf, which has the same behavior but rejects null.
throwable exactly as-is, if and only if it is an instance of declaredType.
|
| public static void | propagateIfPossible(Throwable throwable)
Deprecated
Use
Propagates throwIfUnchecked, which has the same behavior but rejects null.
throwable exactly as-is, if and only if it is an instance of RuntimeException or Error.
|
| public static <X extends Throwable> void | propagateIfPossible(Throwable
the Throwable to possibly propagate throwable, Class<X> the single checked exception type declared by the calling method declaredType)Propagates |
| public static <X1 extends Throwable, X2 extends Throwable> void | propagateIfPossible(Throwable
the Throwable to possibly propagate throwable, Class<X1> any checked exception type declared by the calling method declaredType1, Class<X2> any other checked exception type declared by the calling method declaredType2)Propagates |
| public static <X extends Throwable> void | throwIfInstanceOf(Throwable throwable, Class<X> declaredType)
Throws |
| public static void |
| getStackTraceDepthMethod | back to summary |
|---|---|
| private static final Method getStackTraceDepthMethod The "getStackTraceDepth" method, only available on some JDKs so we use reflection to find it when available. When this is null, use the slow way. | |
| getStackTraceElementMethod | back to summary |
|---|---|
| private static final Method getStackTraceElementMethod The "getStackTraceElementMethod" method, only available on some JDKs so we use reflection to find it when available. When this is null, use the slow way. | |
| JAVA_LANG_ACCESS_CLASSNAME | back to summary |
|---|---|
| private static final String JAVA_LANG_ACCESS_CLASSNAME JavaLangAccess class name to load using reflection | |
| jla | back to summary |
|---|---|
| private static final Object jla Access to some fancy internal JVM internals. | |
| SHARED_SECRETS_CLASSNAME | back to summary |
|---|---|
| pack-priv static final String SHARED_SECRETS_CLASSNAME SharedSecrets class name to load using reflection | |
| Throwables | back to summary |
|---|---|
| private Throwables() | |
| getCausalChain | back to summary |
|---|---|
| public static List Gets a Here's an example of how it can be used to find specific types of exceptions in the cause chain: Iterables.filter(Throwables.getCausalChain(e), IOException.class));
| |
| getCauseAs | back to summary |
|---|---|
| public static <X extends Throwable> X getCauseAs(Throwable throwable, Class<X> expectedCauseType) Returns Prefer this method instead of manually casting an exception's cause. For example,
| |
| getGetMethod | back to summary |
|---|---|
| private static Method getGetMethod() Returns the Method that can be used to resolve an individual StackTraceElement, or null if that method cannot be found (it is only to be found in fairly recent JDKs).
| |
| getJLA | back to summary |
|---|---|
| private static Object getJLA() Returns the JavaLangAccess class that is present in all Sun JDKs. It is not allowed in AppEngine, and not present in non-Sun JDKs.
| |
| getJlaMethod | back to summary |
|---|---|
private static Method getJlaMethod(String name, Class<?>... parameterTypes) throws ThreadDeath
| |
| getRootCause | back to summary |
|---|---|
| public static Throwable getRootCause(Throwable throwable) Returns the innermost cause of
assertEquals("Unable to assign a customer id", Throwables.getRootCause(e).getMessage());
| |
| getSizeMethod | back to summary |
|---|---|
| private static Method getSizeMethod(Object jla) Returns the Method that can be used to return the size of a stack, or null if that method
cannot be found (it is only to be found in fairly recent JDKs). Tries to test method See Throwables#lazyStackTrace throws UnsupportedOperationException.
| |
| getStackTraceAsString | back to summary |
|---|---|
| public static String getStackTraceAsString(Throwable throwable) Returns a string containing the result of
| |
| invokeAccessibleNonThrowingMethod | back to summary |
|---|---|
private static Object invokeAccessibleNonThrowingMethod(Method method, Object receiver, Object... params)
| |
| jlaStackTrace | back to summary |
|---|---|
private static List
| |
| lazyStackTrace | back to summary |
|---|---|
| public static List Deprecated This method is equivalent to Returns the stack trace of The expected (but not guaranteed) performance of the special implementation differs from
Note The special implementation does not respect calls to
| |
| lazyStackTraceIsLazy | back to summary |
|---|---|
| public static boolean lazyStackTraceIsLazy()
Deprecated This method always returns false on JDK versions past JDK 8 and on all Android versions. Returns whether
| |
| propagate | back to summary |
|---|---|
| public static RuntimeException propagate(Throwable throwable)
Deprecated Use Propagates This method always throws an exception. The
T doSomething() {
try {
return someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
return handle(e);
} catch (Throwable t) {
throw Throwables.propagate(t);
}
}
| |
| propagateIfInstanceOf | back to summary |
|---|---|
| public static <X extends Throwable> void propagateIfInstanceOf(Throwable throwable, Class<X> declaredType) throws Class-:X
Deprecated Use Propagates
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
Throwables.propagateIfInstanceOf(t, SQLException.class);
throw Throwables.propagate(t);
}
| |
| propagateIfPossible | back to summary |
|---|---|
| public static void propagateIfPossible(Throwable throwable)
Deprecated Use Propagates
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (Throwable t) {
Throwables.propagateIfPossible(t);
throw new RuntimeException("unexpected", t);
}
| |
| propagateIfPossible | back to summary |
|---|---|
| public static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType) throws Class-:X Propagates
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (Throwable t) {
Throwables.propagateIfPossible(t, OtherException.class);
throw new RuntimeException("unexpected", t);
}
| |
| propagateIfPossible | back to summary |
|---|---|
| public static <X1 extends Throwable, X2 extends Throwable> void propagateIfPossible(Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) throws Class-:X1, Class-:X2 Propagates
| |
| throwIfInstanceOf | back to summary |
|---|---|
| public static <X extends Throwable> void throwIfInstanceOf(Throwable throwable, Class<X> declaredType) throws Class-:X Throws
for (Foo foo : foos) {
try {
foo.bar();
} catch (BarException | RuntimeException | Error t) {
failure = t;
}
}
if (failure != null) {
throwIfInstanceOf(failure, BarException.class);
throwIfUnchecked(failure);
throw new AssertionError(failure);
}
| |
| throwIfUnchecked | back to summary |
|---|---|
| public static void throwIfUnchecked(Throwable throwable) Throws
for (Foo foo : foos) {
try {
foo.bar();
} catch (RuntimeException | Error t) {
failure = t;
}
}
if (failure != null) {
throwIfUnchecked(failure);
throw new AssertionError(failure);
}
| |