Skip to content

Instantly share code, notes, and snippets.

@verhas
Created August 25, 2017 10:43
Show Gist options
  • Save verhas/a2ae93fc8ee14746b54df767e54ab9ce to your computer and use it in GitHub Desktop.
Save verhas/a2ae93fc8ee14746b54df767e54ab9ce to your computer and use it in GitHub Desktop.

Revisions

  1. verhas created this gist Aug 25, 2017.
    37 changes: 37 additions & 0 deletions RuntTimeExceptionWrapper.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    package packt.java9.network.connect;

    import java.util.function.Function;

    public class RuntTimeExceptionWrapper {
    public static <T> T lame(ExceptionalSupplier<T> z) {
    try {
    return z.apply();
    } catch (Exception e) {
    throw new WrapperException(e);
    }
    }

    public static <T, R> Function<T, R> lame(ExceptionalFunction<T, R> f) {
    return (T r) -> {
    try {
    return f.apply(r);
    } catch (Exception e) {
    throw new WrapperException(e);
    }
    };
    }

    public interface ExceptionalSupplier<T> {
    T apply() throws Exception;
    }

    public interface ExceptionalFunction<T, R> {
    R apply(T r) throws Exception;
    }

    public static class WrapperException extends RuntimeException {
    WrapperException(Exception e) {
    super(e);
    }
    }
    }