/** * @param toThrow The throwable that will be thrown, unwrapped and unchecked; must not be null * @throws NullPointerException if toThrow is null */ public static void throwUnchecked(final Throwable toThrow) { throwUnchecked(toThrow, null); }
/** * Because this method throws an unchecked exception, when it is called in a method with a return type the compiler * does not know the method is exiting, requiring a further line to return null or throw an unchecked exception * directly. This generified method allows this to be avoided by tricking the compiler by adding a return statement * as so: * <pre> * String someMethod() { * try { * somethingThatThrowsException(); * } catch (Exception e) { * return throwUnchecked(e, null); // does not actually return, throws the exception * } * } * </pre> * @param toThrow The throwable that will be thrown, unwrapped and unchecked; must not be null * @param returnType trick to persuade the compiler that a method returns appropriately - always pass null here * @return Never returns, always throws the passed in exception * @throws NullPointerException if toThrow is null */ public static <T> T throwUnchecked(final Throwable toThrow, final T returnType) { Exceptions.<RuntimeException>doThrowUnchecked(checkNotNull(toThrow)); throw new AssertionError("This code should be unreachable. Something went terribly wrong here!"); }
@Override public Properties apply(final InputStream propertyResource) { try (InputStream closablePropertyResource = propertyResource) { final Properties loadedProperties = new Properties(); loadedProperties.load(closablePropertyResource); return loadedProperties; } catch (IOException ioException) { return throwUnchecked(ioException, null); } } };
@Override public Properties apply(final InputStream propertyResource) { try (InputStream closablePropertyResource = propertyResource) { final Properties loadedProperties = new Properties(); loadedProperties.load(closablePropertyResource); return loadedProperties; } catch (IOException ioException) { return throwUnchecked(ioException, null); } } };
private FluentIterable<FieldFacade> fields() { try { return IDENTITY_FIELDS.get(getClass()); } catch (ExecutionException e) { return throwUnchecked(e.getCause(), null); } }
@Override protected Throwable featureValueOf(final Constructor<?> constructor) { try { constructor.setAccessible(true); constructor.newInstance(); return null; } catch (InvocationTargetException e) { return e.getCause(); } catch (Exception e) { return throwUnchecked(e, null); } finally { constructor.setAccessible(false); } } };
/** * @throws Exception thrown by {@link #perform()} - unchecked */ @Override public final void run() { try { perform(); } catch (Exception e) { throwUnchecked(e); } }
private static <T> T doWithSystemClasses(Callable<T> callable) throws SysOutOverSLF4JSystemJarNotPresentException { try { return callable.call(); } catch (NoClassDefFoundError error) { if (error.getMessage().contains("sysoutslf4j/system")) { throw new SysOutOverSLF4JSystemJarNotPresentException(error); } else { throw error; } } catch (Exception e) { return throwUnchecked(e, null); } }
/** * @throws Exception thrown by the original caller, unwrapped * @return the result of the expression passed to the constructor; this is evaluated the first time this method is called, * subsequent calls return the same result without re-evaluating it. Concurrent calls block until the first execution * is complete. */ @Override public T call() { supplier.run(); try { return getUninterruptibly(supplier); } catch (ExecutionException e) { return throwUnchecked(e.getCause(), null); } }
@SuppressWarnings({ "unchecked", "PMD.AvoidCatchingThrowable" }) private static <ThrowableType extends Throwable> ThrowableType shouldThrow( final Class<ThrowableType> expectedThrowableType, final Optional<String> message, final Runnable workThatShouldThrowThrowable) { try { workThatShouldThrowThrowable.run(); throw new AssertionError(message.or("No exception thrown")); } catch (final Throwable actualThrowableThrown) { final Throwable trueThrowable = extractTrueThrowable(expectedThrowableType, actualThrowableThrown); if (!expectedThrowableType.isInstance(trueThrowable)) { throwUnchecked(actualThrowableThrown); } return (ThrowableType) trueThrowable; } }