com.healthmarketscience.common.util
Class PassthroughException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.lang.RuntimeException
              extended by com.healthmarketscience.common.util.PassthroughException
All Implemented Interfaces:
Serializable

public final class PassthroughException
extends RuntimeException
implements Serializable

This exception is useful for copying internal exceptions within one classloader so that they can be inspected within another classloader (after serialization/deserialization) without throwing ClassNotFoundException (or related exceptions). This is assuming, of course, that this class is available in both classloaders. An example for this would be an exception generated within a remote server, where the client code would not have the actual class of the exception. By "copying" the exception as a PassthroughException, the exception details can be "passed through" the classloader barrier. The receiver will get a complete stacktrace which looks identical to the server-side stacktrace without any of the class definition issues.

Example usage:


 public class MyBean {
   public int computeSomething() throws MyPublicBeanException {
      try {
        // use some internal library to do something
      } catch(InternalImplException e) {
        throw new MyPublicBeanException("Failed doing something",
                                        PassthroughException.create(e));
      }
   }
 }

 

Author:
James Ahlborn
See Also:
Serialized Form

Constructor Summary
PassthroughException(String originalException, String originalMessage, StackTraceElement[] originalStackTrace, PassthroughException cause)
          Constructs a PassthroughException with the given information.
PassthroughException(Throwable original, PassthroughException cause)
          Constructs a PassthroughException by copying information from the given original exception.
 
Method Summary
static PassthroughException create(Throwable t)
          Constructs a PassthroughException which will print out a virtually identical stacktrace to the given Throwable.
static
<ExType extends Throwable>
ExType
create(Throwable t, Class<ExType> exType, String defaultMsg)
          Creates an exception of the given type, with the given throwable translated to a PassthroughException as the cause.
static
<ExType extends Throwable>
ExType
create(Throwable t, Class<ExType> exType, String defaultMsg, boolean throwError)
          Creates an exception of the given type, with the given throwable translated to a PassthroughException as the cause.
 String getOriginalExceptionName()
           
 String toString()
           
 
Methods inherited from class java.lang.Throwable
fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PassthroughException

public PassthroughException(Throwable original,
                            PassthroughException cause)
Constructs a PassthroughException by copying information from the given original exception.

Parameters:
original - the original non-portable exception
cause - the cause for this exception, (most likely also a PassthroughException)
See Also:
for simple instantiation of a PassthroughException

PassthroughException

public PassthroughException(String originalException,
                            String originalMessage,
                            StackTraceElement[] originalStackTrace,
                            PassthroughException cause)
Constructs a PassthroughException with the given information.

Parameters:
originalException - class name of the original exception
originalMessage - message of the original exception
originalStackTrace - stacktrace of the original exception
cause - the cause for this exception, (most likely also a PassthroughException)
See Also:
for simple instantiation of a PassthroughException
Method Detail

toString

public String toString()
Overrides:
toString in class Throwable

getOriginalExceptionName

public String getOriginalExceptionName()
Returns:
the class name of the original exception wrapped by this PassthroughException

create

public static PassthroughException create(Throwable t)
Constructs a PassthroughException which will print out a virtually identical stacktrace to the given Throwable. This method will deeply "copy" all causes within the given Throwable, returning a cause chain of instances of PassthroughException. Thus, the returned object will have no class dependencies outside of the JDK and this class.

Note, if the given throwable is already a PassthroughException, it will be returned as-is.

Parameters:
t - the throwable to deeply "copy".
Returns:
a deep "copy" of the given Throwable, where all of the exception classes are actually PassthroughException instances. Will return null if given null.

create

public static <ExType extends Throwable> ExType create(Throwable t,
                                                       Class<ExType> exType,
                                                       String defaultMsg)
Creates an exception of the given type, with the given throwable translated to a PassthroughException as the cause. If the top level exception of the given cause chain is an instance of the given exception type, it will be copied (and the result exception will be of this type), otherwise a new exception of the given type will be created.

This method will throw Errors immediately, as-is.

Parameters:
t - throwable cause to wrap as a PassthroughException
exType - the returned exception will be a subclass of this type. This type (and it's subclasses) must have a public constructor like ExType(String, Throwable).
defaultMsg - if the top exception is not of exType and this message is non-null, this will be the message of the resulting exception, otherwise the message of the top PassthroughException will be used (or "" if none).
Returns:
an exception of the given type (possibly a subclass) where all nested causes have been translated to PassthroughExceptions. Will return null if given null.

create

public static <ExType extends Throwable> ExType create(Throwable t,
                                                       Class<ExType> exType,
                                                       String defaultMsg,
                                                       boolean throwError)
Creates an exception of the given type, with the given throwable translated to a PassthroughException as the cause. If the top level exception of the given cause chain is an instance of the given exception type, it will be copied (and the result exception will be of this type), otherwise a new exception of the given type will be created.

Errors may optionally be thrown immediately, as-is, or wrapped as any other cause.

Parameters:
t - throwable cause to wrap as a PassthroughException
exType - the returned exception will be a subclass of this type. This type (and it's subclasses) must have a public constructor like ExType(String, Throwable).
defaultMsg - if the top exception is not of exType and this message is non-null, this will be the message of the resulting exception, otherwise the message of the top PassthroughException will be used (or "" if none).
throwError - iff true, Errors will be thrown immediately, as-is, otherwise they will be treated like any other cause.
Returns:
an exception of the given type (possibly a subclass) where all nested causes have been translated to PassthroughExceptions. Will return null if given null.


Copyright © 2006-2016 Health Market Science. All Rights Reserved.