Throwables { public static <T, U extends RuntimeException, C extends Exception> T withCheckedException(Class<U> uncheckedExceptionClazz , Class<C> checkedExceptionClazz, Supplier<T> supplier) throws C { try { return supplier.get(); } catch (RuntimeException e) { if ( uncheckedExceptionClazz.getClass().isInstance(e.getClass())) { try { C checkedException; try { Constructor<C> declaredConstructor = checkedExceptionClazz.getDeclaredConstructor(String.class, Throwable.class); checkedException = declaredConstructor.newInstance(e.getMessage(), e); } catch (NoSuchMethodException e1) { checkedException = checkedExceptionClazz.newInstance(); checkedException.initCause(e); } throw checkedException; } catch (InstantiationException|IllegalAccessException|InvocationTargetException e1) { throw new RuntimeException("Could not instantiate checked exception: ", e1); } } throw e; } }  static String toString( Throwable t ); static T createMultiException(List<T> exceptions); static C createMultiException(C exception, List<Throwable> throwables); static T withCheckedException(Class<U> uncheckedExceptionClazz , Class<C> checkedExceptionClazz, Supplier<T> supplier);  }
@Test(expected = MyCheckedException.class) public void testWithCheckedExceptionReturningBoolean() throws Exception { Throwables.withCheckedException(MyUncheckedException.class, MyCheckedException.class, () -> myBooleanUncheckedFunction()); }