001: // Copyright (c) 1999, 2003 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.mapping;
005:
006: /** Encapsulate some Exception inside a RuntimeException.
007: * Inspired by org.xml.sax.SAXException written by David Megginson.
008: */
009:
010: public class WrappedException extends RuntimeException {
011: /**
012: * Create a new WrappedException.
013: *
014: * @param message The error or warning message.
015: */
016: public WrappedException(String message) {
017: this .message = message;
018: }
019:
020: /**
021: * Create a new WrappedException wrapping an existing exception.
022: *
023: * <p>The existing exception will be embedded in the new
024: * one, and its message will become the default message for
025: * the WrappedException.</p>
026: *
027: * @param e The exception to be wrapped in a WrappedException.
028: */
029: public WrappedException(Throwable e) {
030: super ();
031: this .message = null;
032: initCause(e);
033: }
034:
035: /**
036: * Create a new WrappedException from an existing exception.
037: *
038: * <p>The existing exception will be embedded in the new
039: * one, but the new exception will have its own message.</p>
040: *
041: * @param message The detail message.
042: * @param e The exception to be wrapped in a WrappedException.
043: */
044: public WrappedException(String message, Throwable e) {
045: super ();
046: this .message = message;
047: initCause(e);
048: }
049:
050: /**
051: * Return a detail message for this exception.
052: *
053: * <p>If there is a embedded exception, and if the WrappedException
054: * has no detail message of its own, this method will return
055: * the detail message from the embedded exception.</p>
056: *
057: * @return The error or warning message.
058: */
059: public String getMessage() {
060: Throwable cause;
061: if (message == null && (cause = getCause()) != null)
062: return cause.getMessage();
063: else
064: return this .message;
065: }
066:
067: /**
068: * Return the embedded exception, if any.
069: *
070: * @return The embedded exception, or null if there is none.
071: */
072: public Throwable getException() {
073: return getCause();
074: }
075:
076: /**
077: * Convert this exception to a string.
078: *
079: * @return A string version of this exception.
080: */
081: public String toString() {
082: return getMessage();
083: }
084:
085: /* The initCause/getCause functionality was added in JDK 1.4.
086: It is available in gcj 3.3, so we could perhaps put it in a JAVA1 block,
087: but I'm not quite ready for that yet.
088: Future: BEGIN a JAVA1 section */
089: public Throwable initCause(Throwable cause) {
090: exception = cause;
091: return this ;
092: }
093:
094: public Throwable getCause() {
095: return exception;
096: }
097:
098: private Throwable exception;
099: /* Future: END a JAVA1 section. */
100: private String message;
101: }
|