com.twitter.common.base
Class ExceptionTransporter<E extends Exception>

java.lang.Object
  extended by com.twitter.common.base.ExceptionTransporter<E>
Type Parameters:
E - The type of checked exception the ExceptionTransported can transport

public class ExceptionTransporter<E extends Exception>
extends Object

A utility for transporting checked exceptions across boundaries that do not allow for checked exception propagation.


Constructor Summary
ExceptionTransporter()
           
 
Method Summary
static
<T,X extends Exception>
T
guard(Function<ExceptionTransporter<X>,T> work)
          Guards a unit of work that internally can generate checked exceptions.
 RuntimeException transport(E checked)
          Throws the given checked exception across a boundary that does not allow checked exceptions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExceptionTransporter

public ExceptionTransporter()
Method Detail

guard

public static <T,X extends Exception> T guard(Function<ExceptionTransporter<X>,T> work)
               throws X extends Exception
Guards a unit of work that internally can generate checked exceptions. Callers wrap up the work in a function that rethrows any checked exceptions using the supplied ExceptionTransporter. Guard will ensure the original exception is unwrapped an re-thrown.

Type Parameters:
T - The type returned by the unit of work when it successfully completes.
X - The type of checked exception that the unit of work wishes to guard.
Parameters:
work - The unit of work that guards its checked exceptions.
Returns:
the result of the unit of work if no excpetions are thrown
Throws:
X - when the unit of work uses the ExceptionTransporter to throw a checked exception
X extends Exception

transport

public RuntimeException transport(E checked)
Throws the given checked exception across a boundary that does not allow checked exceptions. Although a RuntimeException is returned by this method signature, the method never actually completes normally. The return type does allow the following usage idiom however:
 public String apply(ExceptionTransporter transporter) {
   try {
     return doChecked();
   } catch (CheckedException e) {
     // Although transport internally throws and does not return, we satisfy the compiler that
     // our method returns a value or throws by pretending to throw the RuntimeException that
     // never actually gets returned by transporter.transport(...)
     throw transporter.transport(e);
   }
 }
 

Parameters:
checked - The checked exception to transport.
Returns:
A RuntimeException that can be thrown to satisfy the compiler at the call site