/** * Evaluates the given {@code runnable}. If an exception is generated, it will be wrapped using the * thrown exception using the specified {@code wrapper}.<p> * * This variant is an alias of {@link #wrap(CheckedRunnable, Function)} that avoids method ambiguity * when a {@link CheckedRunnable} is being used. * * @param <X> Exception type. * @param runnable The runnable whose exceptions to trap. * @param wrapper The handler for trapped exceptions, returning a wrapped exception of type {@code X}. * @throws X If an exception occurs. */ public static <X extends Throwable> void wrapRunnable(CheckedRunnable<?> runnable, Function<Throwable, ? extends X> wrapper) throws X { wrap(runnable, wrapper); }
/** * Closes a given {@link Closeable} instance, wrapping any potential {@link IOException} * in an unchecked {@link RuntimeIOException}. * * @param closeable The {@link Closeable} to close. */ public static void closeUnchecked(Closeable closeable) { Exceptions.wrapStrict(closeable::close, RuntimeIOException::new); } }
/** * Evaluates the given {@code supplier}, returning the supplied value if successful, or otherwise wrapping the * thrown exception using the specified {@code wrapper}.<p> * * This variant is an alias of {@link #wrap(CheckedSupplier, Function)} that avoids method ambiguity * when a {@link CheckedSupplier} is being used. * * @param <T> Return type. * @param <X> Exception type. * @param supplier The supplier whose exceptions to trap. * @param wrapper The handler for trapped exceptions, returning a wrapped exception of type {@code X}. * @return The result of evaluating the supplier. * @throws X If an exception occurs. */ public static <T, X extends Throwable> T wrapSupplier(CheckedSupplier<? extends T, ?> supplier, Function<Throwable, ? extends X> wrapper) throws X { return wrap(supplier, wrapper); }
/** * Evaluates the given {@code supplier}, returning the supplied value if successful, or otherwise wrapping the * thrown exception using the specified {@code wrapper}. * * @param <T> Return type. * @param <X> Exception type. * @param supplier The supplier whose exceptions to trap. * @param wrapper The handler for trapped exceptions, returning a wrapped exception of type {@code X}. * @return The result of evaluating the supplier. * @throws X If an exception occurs. */ public static <T, X extends Throwable> T wrap(CheckedSupplier<? extends T, ?> supplier, Function<Throwable, ? extends X> wrapper) throws X { return wrapStrict(supplier, wrapper); }
/** * Parses an ISO 8601 encoded date/time using a given formatter. * * @param formatter The formatter to use. * @param encoded The ISO 8601 encoded date/time. * @return The parsed {@link Date}. * @throws Iso8601ParseException If parsing failed with an error. */ public static Date parse(Iso8601Formatter formatter, String encoded) throws Iso8601ParseException { return Exceptions.wrap(() -> formatter.parse(encoded), Iso8601ParseException::new); }
/** * A strict form of {@link #wrap(CheckedRunnable, Function)} that requires that the exception * wrapper takes a subtype of the exception thrown by the runnable block. * * @param <W> Exception type thrown by the runnable, and input to the wrapper. * @param <X> Exception type thrown by the wrapper. * @param runnable The runnable whose exception to trap. * @param wrapper The handler for trapped exceptions, returning a wrapped exception of type {@code X}. * @throws X If an exception occurs. */ public static <W extends Throwable, X extends Throwable> void wrapStrict(CheckedRunnable<? extends W> runnable, Function<? super W, ? extends X> wrapper) throws X { wrapStrict(() -> { runnable.run(); return null; }, wrapper); }
/** * An unchecked variant of {@link #parse(Iso8601Formatter, String)} throwing a * {@link RuntimeIso8601ParseException} in the event of a parse error. * * @param formatter The formatter to use. * @param encoded The ISO 8601 encoded date/time. * @return The parsed {@link Date}. */ public static Date parseUnchecked(Iso8601Formatter formatter, String encoded) { return Exceptions.wrap(() -> formatter.parse(encoded), RuntimeIso8601ParseException::new); }
@Override public LogConfig get() { synchronized (cacheLock) { if (cachedConfig == null) { final Properties props = Exceptions.wrap(propsLoader::get, PropertiesLoadException::new); cachedConfig = loadConfig(props); } } return cachedConfig; }
private static LogService instantiateLogService(String logServiceClassName) { return Exceptions.wrap(() -> Classes.cast(Class.forName(logServiceClassName).getDeclaredConstructor().newInstance()), ServiceInstantiationException::new); }
public void configure(Configurator c) { Exceptions.wrap(() -> c.apply(this), RuntimeException::new); }
/** * Evaluates the given {@code runnable}. If an exception is generated, it will be wrapped using the * thrown exception using the specified {@code wrapper}. * * @param <X> Exception type. * @param runnable The runnable whose exceptions to trap. * @param wrapper The handler for trapped exceptions, returning a wrapped exception of type {@code X}. * @throws X If an exception occurs. */ public static <X extends Throwable> void wrap(CheckedRunnable<?> runnable, Function<Throwable, ? extends X> wrapper) throws X { wrap(() -> { runnable.run(); return null; }, wrapper); }
final List<Future<V>> submissions = new ArrayList<>(); source.forEach(item -> { submissions.add(executor.submit(() -> Exceptions.wrap(() -> mapper.apply(item), CapturedException::new))); });