package eu.dnetlib.dhp.common; import eu.dnetlib.dhp.common.FunctionalInterfaceSupport.ThrowingRunnable; import eu.dnetlib.dhp.common.FunctionalInterfaceSupport.ThrowingSupplier; /** Exception handling utility methods. */ public class ThrowingSupport { private ThrowingSupport() {} /** * Executes given runnable and rethrows any exceptions as RuntimeException. * * @param fn Runnable to be executed * @param Type of exception thrown */ public static void rethrowAsRuntimeException(ThrowingRunnable fn) { try { fn.run(); } catch (Exception e) { throw new RuntimeException(e); } } /** * Executes given runnable and rethrows any exceptions as RuntimeException with custom message. * * @param fn Runnable to be executed * @param msg Message to be set for rethrown exception * @param Type of exception thrown */ public static void rethrowAsRuntimeException( ThrowingRunnable fn, String msg) { try { fn.run(); } catch (Exception e) { throw new RuntimeException(msg, e); } } /** * Executes given supplier and rethrows any exceptions as RuntimeException. * * @param fn Supplier to be executed * @param Type of returned value * @param Type of exception thrown * @return Result of supplier execution */ public static T rethrowAsRuntimeException(ThrowingSupplier fn) { try { return fn.get(); } catch (Exception e) { throw new RuntimeException(e); } } /** * Executes given supplier and rethrows any exceptions as RuntimeException with custom message. * * @param fn Supplier to be executed * @param msg Message to be set for rethrown exception * @param Type of returned value * @param Type of exception thrown * @return Result of supplier execution */ public static T rethrowAsRuntimeException( ThrowingSupplier fn, String msg) { try { return fn.get(); } catch (Exception e) { throw new RuntimeException(msg, e); } } }