• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.internal;
2 
3 /**
4  * Miscellaneous functions dealing with {@code Throwable}.
5  *
6  * @author kcooney@google.com (Kevin Cooney)
7  * @since 4.12
8  */
9 public final class Throwables {
10 
Throwables()11     private Throwables() {
12     }
13 
14     /**
15      * Rethrows the given {@code Throwable}, allowing the caller to
16      * declare that it throws {@code Exception}. This is useful when
17      * your callers have nothing reasonable they can do when a
18      * {@code Throwable} is thrown. This is declared to return {@code Exception}
19      * so it can be used in a {@code throw} clause:
20      * <pre>
21      * try {
22      *   doSomething();
23      * } catch (Throwable e} {
24      *   throw Throwables.rethrowAsException(e);
25      * }
26      * doSomethingLater();
27      * </pre>
28      *
29      * @param e exception to rethrow
30      * @return does not return anything
31      * @since 4.12
32      */
rethrowAsException(Throwable e)33     public static Exception rethrowAsException(Throwable e) throws Exception {
34         Throwables.<Exception>rethrow(e);
35         return null; // we never get here
36     }
37 
38     @SuppressWarnings("unchecked")
rethrow(Throwable e)39     private static <T extends Throwable> void rethrow(Throwable e) throws T {
40         throw (T) e;
41     }
42 }
43