/** * 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!"); }