guava/src/com/google/common/base/Throwables.java
@GwtIncompatible // Class.cast, Class.isInstance
  public static <X extends Throwable> void throwIfInstanceOf(
      Throwable throwable, Class<X> declaredType) throws X {
    checkNotNull(throwable);
    if (declaredType.isInstance(throwable)) {
      throw declaredType.cast(throwable);
    }
  }
@GwtIncompatible // Class.cast, Class.isInstance
  public static <X extends Throwable> void throwIfInstanceOf(
      Throwable throwable, Class<X> declaredType) throws X {
    checkNotNull(throwable);
    if (declaredType.isInstance(throwable)) {
      throw declaredType.cast(throwable);
    }
  }
@ GwtIncompatible // Class.cast, Class.isInstance public static < X extends Throwable > void throwIfInstanceOf ( Throwable throwable , Class < X > declaredType ) throws X { checkNotNull ( throwable ) ; if ( declaredType . isInstance ( throwable ) ) { throw declaredType . cast ( throwable ) ; } }
Throws {@code throwable} if it is an instance of {@code declaredType}. Example usage:

<pre>
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);
}
</pre>

@since 20.0
Throws { @code throwable } if it is an instance of { @code declaredType } . Example usage :