com.rt.storage.api.client.util.Throwables Maven / Gradle / Ivy
package com.rt.storage.api.client.util;
/**
* Static utility methods pertaining to instances of {@link Throwable}.
*
* @since 1.14
* @author Yaniv Inbar
*/
public final class Throwables {
/**
* Propagates {@code throwable} as-is if it is an instance of {@link RuntimeException} or {@link
* Error}, or else as a last resort, wraps it in a {@code RuntimeException} then propagates.
*
* This method always throws an exception. The {@code 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);
* }
* }
*
*
* @param throwable the Throwable to propagate
* @return nothing will ever be returned; this return type is only for your convenience, as
* illustrated in the example above
*/
public static RuntimeException propagate(Throwable throwable) {
return com.google.common.base.Throwables.propagate(throwable);
}
/**
* Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link
* RuntimeException} or {@link Error}. Example usage:
*
*
* try {
* someMethodThatCouldThrowAnything();
* } catch (IKnowWhatToDoWithThisException e) {
* handle(e);
* } catch (Throwable t) {
* Throwables.propagateIfPossible(t);
* throw new RuntimeException("unexpected", t);
* }
*
*
* @param throwable throwable (may be {@code null})
*/
public static void propagateIfPossible(Throwable throwable) {
if (throwable != null) {
com.google.common.base.Throwables.throwIfUnchecked(throwable);
}
}
/**
* Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link
* RuntimeException}, {@link Error}, or {@code declaredType}. Example usage:
*
*
* try {
* someMethodThatCouldThrowAnything();
* } catch (IKnowWhatToDoWithThisException e) {
* handle(e);
* } catch (Throwable t) {
* Throwables.propagateIfPossible(t, OtherException.class);
* throw new RuntimeException("unexpected", t);
* }
*
*
* @param throwable throwable (may be {@code null})
* @param declaredType the single checked exception type declared by the calling method
*/
public static void propagateIfPossible(
Throwable throwable, Class declaredType) throws X {
com.google.common.base.Throwables.propagateIfPossible(throwable, declaredType);
}
private Throwables() {}
}