//------------------------------------------------------------------------- /// <summary> /// Wraps a block of code, converting checked exceptions to unchecked. /// <pre> /// Unchecked.wrap(() -> { /// // any code that throws a checked exception /// } /// </pre> /// <para> /// If a checked exception is thrown it is converted to an <seealso cref="UncheckedIOException"/> /// or <seealso cref="RuntimeException"/> as appropriate. /// /// </para> /// </summary> /// <param name="block"> the code block to wrap </param> /// <exception cref="UncheckedIOException"> if an IO exception occurs </exception> /// <exception cref="RuntimeException"> if an exception occurs </exception> public static void wrap(CheckedRunnable block) { try { block(); } catch (Exception ex) { throw propagate(ex); } }
//------------------------------------------------------------------------- /// <summary> /// Converts checked exceptions to unchecked based on the {@code Runnable} interface. /// <para> /// This wraps the specified runnable returning an instance that handles checked exceptions. /// If a checked exception is thrown it is converted to an <seealso cref="UncheckedIOException"/> /// or <seealso cref="RuntimeException"/> as appropriate. /// /// </para> /// </summary> /// <param name="runnable"> the runnable to be decorated </param> /// <returns> the runnable instance that handles checked exceptions </returns> public static ThreadStart runnable(CheckedRunnable runnable) { return(() => { try { runnable(); } catch (Exception ex) { throw propagate(ex); } }); }