diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-04-03 10:29:05 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-04-03 10:29:05 +0100 |
| commit | 1dd585ff91b38fc3caf679c8df0bee9695e1ae09 (patch) | |
| tree | e3768593ef24361786873e3bb3ac485f01d52fd8 /src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java | |
| parent | b367d011903e5c249c6bbf614cdc429ca6dd8604 (diff) | |
Rethrow unwrapped IOException in public methods
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java b/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java new file mode 100644 index 0000000..155490f --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java @@ -0,0 +1,32 @@ +package org.zwobble.mammoth.internal.util; + +import java.io.IOException; + +public class PassThroughException extends RuntimeException { + public interface SupplierWithException<T, E extends Throwable> { + public T get() throws E; + } + + public static <T> T wrap(SupplierWithException<T, IOException> supplier) { + try { + return supplier.get(); + } catch (IOException exception) { + throw new PassThroughException(exception); + } + } + + public static <T> T unwrap(SupplierWithException<T, IOException> supplier) throws IOException { + try { + return supplier.get(); + } catch (PassThroughException exception) { + throw exception.exception; + } + } + + private final IOException exception; + + public PassThroughException(IOException exception) { + super(exception); + this.exception = exception; + } +} |
