001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: ChainedException.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023:
024: package com.lutris.util;
025:
026: import java.io.PrintStream;
027: import java.io.PrintWriter;
028:
029: /**
030: * Exception used as a base for creating an exception that has a chain of
031: * exceptions that lead to the derived exception. Very useful for interfaces
032: * where the implementation exception is not known.
033: */
034: public class ChainedException extends Exception implements
035: ChainedThrowable {
036: private Throwable cause;
037:
038: /**
039: * Construct an exception without a specified cause.
040: *
041: * @param msg The message associated with the exception.
042: */
043: public ChainedException(String msg) {
044: super (msg);
045: cause = null;
046: }
047:
048: /**
049: * Construct an exception with an associated causing exception.
050: *
051: * @param msg The message associated with the exception.
052: * @param cause The error or exception that cause this
053: * exception.
054: */
055: public ChainedException(String msg, Throwable cause) {
056: super (msg);
057: this .cause = cause;
058: }
059:
060: /**
061: * Construct an exception from a causing exception.
062: *
063: * @param cause The error or exception that cause this
064: * exception. The message will be take be this object's
065: * messasge.
066: */
067: public ChainedException(Throwable cause) {
068: super (ChainedThrowableUtil.makeMessage(cause));
069: this .cause = cause;
070: }
071:
072: /**
073: * Return the message associated with this exception. If causes
074: * are included, they will be appended to the message.
075: */
076: public String getMessage() {
077: return ChainedThrowableUtil
078: .getMessage(this , super .getMessage());
079: }
080:
081: /**
082: * Get the causing exception associated with this exception.
083: * @return The causing exception or null if no cause is specified.
084: */
085: public Throwable getCause() {
086: return cause;
087: }
088:
089: /**
090: * Prints this ChainedException and its backtrace, and the causes
091: * and their stack traces to the standard error stream.
092: */
093: public void printStackTrace() {
094: super .printStackTrace();
095: ChainedThrowableUtil.printCauseTrace(this );
096: }
097:
098: /**
099: * Prints this ChainedException and its backtrace, and the causes
100: * and their stack traces to the e specified print stream.
101: */
102: public void printStackTrace(PrintStream s) {
103: super .printStackTrace(s);
104: ChainedThrowableUtil.printCauseTrace(this , s);
105: }
106:
107: /**
108: * Prints this ChainedException and its backtrace, and the causes
109: * and their stack traces to the e specified print writer.
110: */
111: public void printStackTrace(PrintWriter s) {
112: super.printStackTrace(s);
113: ChainedThrowableUtil.printCauseTrace(this, s);
114: }
115: }
|