Modifier and Type | Method and Description |
---|---|
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. |
static void |
propagateIfPossible(Throwable throwable)
|
static <X extends Throwable> |
propagateIfPossible(Throwable throwable,
Class<X> declaredType)
Propagates
throwable exactly as-is, if and only if it is an instance of RuntimeException , Error , or declaredType . |
public static RuntimeException propagate(Throwable throwable)
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); } }
throwable
- the Throwable to propagatepublic static void propagateIfPossible(Throwable throwable)
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); }
throwable
- throwable (may be null
)public static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType) throws X extends Throwable
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); }
throwable
- throwable (may be null
)declaredType
- the single checked exception type declared by the calling methodX extends Throwable
Copyright © 2011–2021 Google. All rights reserved.