/** * Converts the given throwable to an {@link Exception} if necessary. * <p> * If {@code throwable} is an {@link Error}, it will be thrown. * <p> * If {@code throwable} is an {@link Exception}, it will be returned unchanged. * <p> * If {@code throwable} is not an {@link Exception}, a newly created {@link UncheckedException} will be returned with the original throwable as the cause and with no message. * * @param throwable the throwable to ensure is an exception * @return a runtime throwable */ public static Exception toException(Throwable throwable) { if (throwable instanceof Error) { throw (Error) throwable; } if (throwable instanceof Exception) { return (Exception) throwable; } else { return new UncheckedException(throwable); } }
/** * Converts the given throwable to a {@link RuntimeException} if necessary. * <p> * If {@code throwable} is an {@link Error}, it will be thrown. * <p> * If {@code throwable} is a {@link RuntimeException}, it will be returned unchanged. * <p> * If {@code throwable} is not a {@link RuntimeException}, a newly created {@link UncheckedException} will be returned with the original throwable as the cause and with no message. * * @param throwable the throwable to ensure is a runtime exception * @return a runtime throwable */ public static RuntimeException uncheck(Throwable throwable) { if (throwable instanceof Error) { throw (Error) throwable; } if (throwable instanceof RuntimeException) { return (RuntimeException) throwable; } else if (throwable instanceof IOException) { throw new UncheckedIOException((IOException) throwable); } else { return new UncheckedException(throwable); } }