Class Throwables (1.44.2)

public final class Throwables

Static utility methods pertaining to instances of Throwable.

NOTE: proxy for the Guava implementation of com.google.common.base.Throwables.

Inheritance

java.lang.Object > Throwables

Static Methods

<X>propagateIfPossible(Throwable throwable, Class<X> declaredType)

public static void <X>propagateIfPossible(Throwable throwable, Class<X> declaredType)

Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException, Error, or declaredType. Example usage:

try { someMethodThatCouldThrowAnything(); } catch (IKnowWhatToDoWithThisException e) { handle(e); } catch (Throwable t) { Throwables.propagateIfPossible(t, OtherException.class); throw new RuntimeException("unexpected", t); }

Parameters
Name Description
throwable Throwable

throwable (may be null)

declaredType Class<X>

the single checked exception type declared by the calling method

Exceptions
Type Description
X

propagate(Throwable throwable)

public static RuntimeException propagate(Throwable throwable)

Propagates throwable as-is if it is an instance of RuntimeException or Error, or else as a last resort, wraps it in a RuntimeException then propagates.

This method always throws an exception. The RuntimeException return type is only for client code to make Java type system happy in case a return value is required by the enclosing method. Example usage:

T doSomething() { try { return someMethodThatCouldThrowAnything(); } catch (IKnowWhatToDoWithThisException e) { return handle(e); } catch (Throwable t) { throw Throwables.propagate(t); } }

Parameter
Name Description
throwable Throwable

the Throwable to propagate

Returns
Type Description
RuntimeException

nothing will ever be returned; this return type is only for your convenience, as illustrated in the example above

propagateIfPossible(Throwable throwable)

public static void propagateIfPossible(Throwable throwable)

Propagates throwable exactly as-is, if and only if it is an instance of RuntimeException or Error. Example usage:

try { someMethodThatCouldThrowAnything(); } catch (IKnowWhatToDoWithThisException e) { handle(e); } catch (Throwable t) { Throwables.propagateIfPossible(t); throw new RuntimeException("unexpected", t); }

Parameter
Name Description
throwable Throwable

throwable (may be null)