MCPcopy Create free account
hub / github.com/antlr/codebuff / Throwables

Class Throwables

corpus/java/training/guava/base/Throwables.java:48–474  ·  view source on GitHub ↗

Static utility methods pertaining to instances of Throwable. See the Guava User Guide entry on Throwables . @author Kevin Bourrillion @author Ben Yu @since 1.0

Source from the content-addressed store, hash-verified

46 * @since 1.0
47 */
48@GwtCompatible(emulated = true)
49public final class Throwables {
50 private Throwables() {}
51
52 /**
53 * Throws {@code throwable} if it is an instance of {@code declaredType}. Example usage:
54 *
55 * <pre>
56 * for (Foo foo : foos) {
57 * try {
58 * foo.bar();
59 * } catch (BarException | RuntimeException | Error t) {
60 * failure = t;
61 * }
62 * }
63 * if (failure != null) {
64 * throwIfInstanceOf(failure, BarException.class);
65 * throwIfUnchecked(failure);
66 * throw new AssertionError(failure);
67 * }
68 * </pre>
69 *
70 * @since 20.0
71 */
72 @GwtIncompatible // Class.cast, Class.isInstance
73 public static <X extends Throwable> void throwIfInstanceOf(
74 Throwable throwable, Class<X> declaredType) throws X {
75 checkNotNull(throwable);
76 if (declaredType.isInstance(throwable)) {
77 throw declaredType.cast(throwable);
78 }
79 }
80
81 /**
82 * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@code
83 * declaredType}. Example usage:
84 *
85 * <pre>
86 * try {
87 * someMethodThatCouldThrowAnything();
88 * } catch (IKnowWhatToDoWithThisException e) {
89 * handle(e);
90 * } catch (Throwable t) {
91 * Throwables.propagateIfInstanceOf(t, IOException.class);
92 * Throwables.propagateIfInstanceOf(t, SQLException.class);
93 * throw Throwables.propagate(t);
94 * }
95 * </pre>
96 *
97 * @deprecated Use {@link #throwIfInstanceOf}, which has the same behavior
98 * but rejects {@code null}. This method is scheduled to be removed in July 2018.
99 */
100 @Deprecated
101 @GwtIncompatible // throwIfInstanceOf
102 public static <X extends Throwable> void propagateIfInstanceOf(
103 @Nullable Throwable throwable, Class<X> declaredType) throws X {
104 if (throwable != null) {
105 throwIfInstanceOf(throwable, declaredType);

Callers

nothing calls this directly

Calls 3

getJLAMethod · 0.95
getGetMethodMethod · 0.95
getSizeMethodMethod · 0.95

Tested by

no test coverage detected