ExceptionUtil { @SuppressWarnings("unchecked") public static <T extends Throwable> void rethrow(Throwable t, Class<T> clz) throws T { if (clz.isAssignableFrom(t.getClass())) throw (T)t; }  static String toString(Throwable th); static String toString(Throwable th, boolean printStack); @SuppressWarnings("unchecked") static void rethrow(Throwable t, Class<T> clz); static void rethrowRuntime(Throwable t);  }
@Test public void test() throws Exception { FileNotFoundException fnfe = new FileNotFoundException(); try { ExceptionUtil.rethrow(fnfe, IOException.class); fail(); } catch (IOException e) { assertTrue("we won !?!", true); } ExceptionUtil.rethrow(fnfe, ClassNotFoundException.class); try { ExceptionUtil.rethrow(fnfe, FileNotFoundException.class); fail(); } catch (FileNotFoundException e) { assertTrue("we won !?!", true); } }