001package org.junit.internal;
002
003/**
004 * Miscellaneous functions dealing with {@code Throwable}.
005 *
006 * @author [email protected] (Kevin Cooney)
007 * @since 4.12
008 */
009public final class Throwables {
010
011    private Throwables() {
012    }
013
014    /**
015     * Rethrows the given {@code Throwable}, allowing the caller to
016     * declare that it throws {@code Exception}. This is useful when
017     * your callers have nothing reasonable they can do when a
018     * {@code Throwable} is thrown. This is declared to return {@code Exception}
019     * so it can be used in a {@code throw} clause:
020     * <pre>
021     * try {
022     *   doSomething();
023     * } catch (Throwable e} {
024     *   throw Throwables.rethrowAsException(e);
025     * }
026     * doSomethingLater();
027     * </pre>
028     *
029     * @param e exception to rethrow
030     * @return does not return anything
031     * @since 4.12
032     */
033    public static Exception rethrowAsException(Throwable e) throws Exception {
034        Throwables.<Exception>rethrow(e);
035        return null; // we never get here
036    }
037
038    @SuppressWarnings("unchecked")
039    private static <T extends Throwable> void rethrow(Throwable e) throws T {
040        throw (T) e;
041    }
042}