From 1dd585ff91b38fc3caf679c8df0bee9695e1ae09 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Apr 2016 10:29:05 +0100 Subject: Rethrow unwrapped IOException in public methods --- .../internal/util/PassThroughException.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java (limited to 'src/main/java/org/zwobble/mammoth/internal/util/PassThroughException.java') 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 { + public T get() throws E; + } + + public static T wrap(SupplierWithException supplier) { + try { + return supplier.get(); + } catch (IOException exception) { + throw new PassThroughException(exception); + } + } + + public static T unwrap(SupplierWithException 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; + } +} -- cgit v1.2.3