/** * Stores the given throwable and rethrows it. It will be rethrown as is if it is an {@code * IOException}, {@code RuntimeException}, {@code Error} or a checked exception of the given type. * Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. <b>Note:</b> Be sure to * declare all of the checked exception types your try block can throw when calling an overload of * this method so as to avoid losing the original exception type. * * <p>This method always throws, and as such should be called as {@code throw closer.rethrow(e, * ...);} to ensure the compiler knows that it will throw. * * @return this method does not return; it always throws * @throws IOException when the given throwable is an IOException * @throws X when the given throwable is of the declared type X */ public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, X { checkNotNull(e); thrown = e; Throwables.propagateIfPossible(e, IOException.class); Throwables.propagateIfPossible(e, declaredType); throw new RuntimeException(e); }
/** * Stores the given throwable and rethrows it. It will be rethrown as is if it is an {@code * IOException}, {@code RuntimeException} or {@code Error}. Otherwise, it will be rethrown wrapped * in a {@code RuntimeException}. <b>Note:</b> Be sure to declare all of the checked exception * types your try block can throw when calling an overload of this method so as to avoid losing * the original exception type. * * <p>This method always throws, and as such should be called as {@code throw closer.rethrow(e);} * to ensure the compiler knows that it will throw. * * @return this method does not return; it always throws * @throws IOException when the given throwable is an IOException */ public RuntimeException rethrow(Throwable e) throws IOException { checkNotNull(e); thrown = e; Throwables.propagateIfPossible(e, IOException.class); throw new RuntimeException(e); }
private OrcCorruptionException propagate(Throwable throwable, String message) { propagateIfPossible(throwable, PrestoException.class); return new OrcCorruptionException(throwable, orcDataSourceId, message); } }
@Override public void close() throws IOException { closed = true; if (throwOnClose != null) { Throwables.propagateIfPossible(throwOnClose, IOException.class); throw new AssertionError(throwOnClose); } } }
@Override public void close() throws Exception { closed = true; if (failure != null) { propagateIfPossible(failure, Exception.class); // not possible throw new AssertionError(failure); } } }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, {@code declaredType1}, or {@code declaredType2}. In the * unlikely case that you have three or more declared checked exception types, you can handle them * all by invoking these methods repeatedly. See usage example in {@link * #propagateIfPossible(Throwable, Class)}. * * @param throwable the Throwable to possibly propagate * @param declaredType1 any checked exception type declared by the calling method * @param declaredType2 any other checked exception type declared by the calling method */ @GwtIncompatible // propagateIfInstanceOf public static <X1 extends Throwable, X2 extends Throwable> void propagateIfPossible( @Nullable Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) throws X1, X2 { checkNotNull(declaredType2); propagateIfInstanceOf(throwable, declaredType1); propagateIfPossible(throwable, declaredType2); }
@GwtIncompatible // propagateIfPossible(Throwable, Class) public void testPropageIfPossible_OneDeclared_null() throws SomeCheckedException { Throwables.propagateIfPossible(null, SomeCheckedException.class); }
@GwtIncompatible // propagateIfPossible public void testPropageIfPossible_null() throws SomeCheckedException { Throwables.propagateIfPossible(null); }
@GwtIncompatible // propagateIfPossible(Throwable, Class, Class) public void testPropageIfPossible_TwoDeclared_null() throws SomeCheckedException { Throwables.propagateIfPossible(null, SomeCheckedException.class, SomeUncheckedException.class); }
@Override public void noneDeclared() { try { methodThatDoesntThrowAnything(); } catch (Throwable t) { Throwables.propagateIfPossible(t); throw new SomeChainingException(t); } } };
@Override public void noneDeclared() { try { methodThatThrowsUnchecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t); throw new SomeChainingException(t); } } };
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatDoesntThrowAnything(); } catch (Throwable t) { // yes, this block is never reached, but for purposes of illustration // we're keeping it the same in each test Throwables.propagateIfPossible(t, SomeCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsChecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t, SomeCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void twoDeclared() throws SomeCheckedException, SomeOtherCheckedException { try { methodThatDoesntThrowAnything(); } catch (Throwable t) { Throwables.propagateIfPossible( t, SomeCheckedException.class, SomeOtherCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void twoDeclared() throws SomeCheckedException, SomeOtherCheckedException { try { methodThatThrowsUnchecked(); } catch (Throwable t) { Throwables.propagateIfPossible( t, SomeCheckedException.class, SomeOtherCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void twoDeclared() throws SomeCheckedException, SomeOtherCheckedException { try { methodThatThrowsOtherChecked(); } catch (Throwable t) { Throwables.propagateIfPossible( t, SomeCheckedException.class, SomeOtherCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void twoDeclared() throws SomeCheckedException, SomeOtherCheckedException { try { methodThatThrowsChecked(); } catch (Throwable t) { Throwables.propagateIfPossible( t, SomeCheckedException.class, SomeOtherCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void noneDeclared() { try { methodThatThrowsUndeclaredChecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t); throw new SomeChainingException(t); } } };
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsUnchecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t, SomeCheckedException.class); throw new SomeChainingException(t); } } };
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsUndeclaredChecked(); } catch (Throwable t) { Throwables.propagateIfPossible(t, SomeCheckedException.class); throw new SomeChainingException(t); } } };