Class ExceptionWrapper

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by ExceptionWrapper
All Implemented Interfaces:
java.io.Serializable, WrappableException
Direct Known Subclasses:
ThincException

public class ExceptionWrapper
extends java.lang.Exception
implements WrappableException

Serves as a base class for WrappableExceptions, to allow exception nesting. Extend this class for your custom service exception case. Alternately, if you don't like the constructors or methods of this exception, implement WrappableException to create your own -- it's nesting behavior will be compatible with this class, allowing interchangable nesting of either without clunky subclassing behavior.

Inside your service, catch low-level exceptions and re-throw them wrapped in your custom exception. This allows simplification of clients who need to catch exceptions from your service, but doesn't sacrifice the level of detail represented by the wrapped exception, since it is accessible via getEnclosedException().

For example, if you've created a custom "MyServiceException" that extends either this Exception, or, alternately, implements the WrapperableException interface, when using it the internals of your service might look like:

  try {
      someOperationWhichThrowsAnException();
  } catch (SomeLowLevelException ex) {
      throw new MyServiceException("Something jiggy is up while doing this!",
          ex);
  }
 
A client, after catching "MyServiceException", can either extract the enclosed exception or call methods on this exception, like toString(), to derive its internals and save them to a log, email them to a developer, or display them to a user.

The exception classnames and messages are number 1 to N, from outermost to innermost. The innermost also has an appended call stack trace to allow pinpointing the source of the exception.

Author:
Leo Kim (lnk2101@columbia.edu)
See Also:
WrappableException, Serialized Form

Field Summary
private  java.lang.Exception nestedException
           
 
Constructor Summary
ExceptionWrapper()
          Constructs a ExceptionWrapper with no specific message.
ExceptionWrapper(java.lang.Exception e)
          Constructs an ExceptionWrapper with no specific message and wraps the specified Exception which caused the problem.
ExceptionWrapper(java.lang.String desc)
          Constructs an ExceptionWrapper with the specified message.
ExceptionWrapper(java.lang.String desc, java.lang.Exception e)
          Constructs an ExceptionWrapper with the specified message and wraps the specified Exception which caused the problem.
 
Method Summary
protected static java.lang.String getClassName(java.lang.Object o)
          Helper to get name of Class of the given Object.
 java.lang.Exception getEnclosedException()
          Allow access to enclosed exception.
protected static java.lang.String getStackTraceAsString(java.lang.Exception e)
          Gets an exception's stack trace as a String :NOTE: this could be moved to a utils class
 void printStackTrace()
          Overriden method from Throwable.
 void printStackTrace(java.io.PrintStream s)
          Overriden method from Throwable.
 void printStackTrace(java.io.PrintWriter s)
          Overriden method from Throwable.
 java.lang.String toString()
          Allow access to messages and stack trace of this and any enclosed exception, including nested SQLExceptions.
 
Methods inherited from class java.lang.Throwable
fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, setStackTrace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nestedException

private java.lang.Exception nestedException
Constructor Detail

ExceptionWrapper

public ExceptionWrapper()
Constructs a ExceptionWrapper with no specific message.


ExceptionWrapper

public ExceptionWrapper(java.lang.String desc)
Constructs an ExceptionWrapper with the specified message.


ExceptionWrapper

public ExceptionWrapper(java.lang.String desc,
                        java.lang.Exception e)
Constructs an ExceptionWrapper with the specified message and wraps the specified Exception which caused the problem.

Parameters:
desc - a String representing the description of the problem that occurred
e - Exception to be wrapped

ExceptionWrapper

public ExceptionWrapper(java.lang.Exception e)
Constructs an ExceptionWrapper with no specific message and wraps the specified Exception which caused the problem.

Parameters:
e - Exception which caused the problem
Method Detail

getEnclosedException

public java.lang.Exception getEnclosedException()
Allow access to enclosed exception.

Specified by:
getEnclosedException in interface WrappableException
Returns:
enclosed Exception, may be null

toString

public java.lang.String toString()
Allow access to messages and stack trace of this and any enclosed exception, including nested SQLExceptions.

Overrides:
toString in class java.lang.Throwable
Returns:
exception names, message and stack trace for this and enclosed exception. If enclosed exception implements WrapperableException, its name and message is appended and recursion continues. If enclosed exception is a SQLException, its name and message is appended and recursion continues. Only the deepest exception appends a stack trace. See class level documentation for an example of this output.

getStackTraceAsString

protected static java.lang.String getStackTraceAsString(java.lang.Exception e)
Gets an exception's stack trace as a String :NOTE: this could be moved to a utils class

Parameters:
e - the exception
Returns:
the stack trace of the exception

getClassName

protected static java.lang.String getClassName(java.lang.Object o)
Helper to get name of Class of the given Object. :NOTE: this could be moved to a utils class

Parameters:
o - the Object to derive the class name from.
Returns:
the class name of the given Object. Return empty String if passed null.

printStackTrace

public void printStackTrace(java.io.PrintWriter s)
Overriden method from Throwable. Prints stack trace correctly of the exception that is stored (wrapped) in this class. Prints messages (getMessage()) of all WrapperableException-s and prints stacktrace of the wrapped exception. For details on this message see Throwable.

Overrides:
printStackTrace in class java.lang.Throwable
See Also:
Throwable.printStackTrace(PrintWriter)

printStackTrace

public void printStackTrace(java.io.PrintStream s)
Overriden method from Throwable. Prints stack trace correctly of the exception that is stored (wrapped) in this class. Prints messages (getMessage()) of all WrappableExceptions and prints stacktrace of the wrapped exception. For details on this message see Throwable.

Overrides:
printStackTrace in class java.lang.Throwable
See Also:
Throwable.printStackTrace(PrintStream)

printStackTrace

public void printStackTrace()
Overriden method from Throwable. Prints stack trace correctly of the exception that is stored (wrapped) in this class. Prints messages (getMessage()) of all WrapperableException-s and prints stacktrace of the wrapped exception. For details on this message see Throwable.

Overrides:
printStackTrace in class java.lang.Throwable
See Also:
Throwable.printStackTrace()