001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowledgement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgement may appear in the software itself,
024: * if and wherever such third-party acknowledgements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Software Foundation.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: */
054: package org.hibernate.exception;
055:
056: import java.io.PrintStream;
057: import java.io.PrintWriter;
058: import java.io.ObjectOutputStream;
059: import java.io.IOException;
060:
061: import antlr.RecognitionException;
062:
063: /**
064: * The base class of all runtime exceptions which can contain other
065: * exceptions.
066: *
067: * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
068: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
069: * @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
070: * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
071: * @version $Id: NestableRuntimeException.java 8137 2005-09-09 15:21:10Z epbernard $
072: * @see org.apache.commons.lang.exception.NestableException
073: * @since 1.0
074: */
075: public class NestableRuntimeException extends RuntimeException
076: implements Nestable {
077:
078: /**
079: * The helper instance which contains much of the code which we
080: * delegate to.
081: */
082: protected NestableDelegate delegate = new NestableDelegate(this );
083:
084: /**
085: * Holds the reference to the exception or error that caused
086: * this exception to be thrown.
087: */
088: private Throwable cause = null;
089:
090: /**
091: * Constructs a new <code>NestableRuntimeException</code> without specified
092: * detail message.
093: */
094: public NestableRuntimeException() {
095: super ();
096: }
097:
098: /**
099: * Constructs a new <code>NestableRuntimeException</code> with specified
100: * detail message.
101: *
102: * @param msg the error message
103: */
104: public NestableRuntimeException(String msg) {
105: super (msg);
106: }
107:
108: /**
109: * Constructs a new <code>NestableRuntimeException</code> with specified
110: * nested <code>Throwable</code>.
111: *
112: * @param cause the exception or error that caused this exception to be
113: * thrown
114: */
115: public NestableRuntimeException(Throwable cause) {
116: super ();
117: this .cause = cause;
118: }
119:
120: /**
121: * Constructs a new <code>NestableRuntimeException</code> with specified
122: * detail message and nested <code>Throwable</code>.
123: *
124: * @param msg the error message
125: * @param cause the exception or error that caused this exception to be
126: * thrown
127: */
128: public NestableRuntimeException(String msg, Throwable cause) {
129: super (msg);
130: this .cause = cause;
131: }
132:
133: public Throwable getCause() {
134: return cause;
135: }
136:
137: /**
138: * Returns the detail message string of this throwable. If it was
139: * created with a null message, returns the following:
140: * ( cause==null ? null : cause.toString( ).
141: */
142: public String getMessage() {
143: if (super .getMessage() != null) {
144: return super .getMessage();
145: } else if (cause != null) {
146: return cause.toString();
147: } else {
148: return null;
149: }
150: }
151:
152: public String getMessage(int index) {
153: if (index == 0) {
154: return super .getMessage();
155: } else {
156: return delegate.getMessage(index);
157: }
158: }
159:
160: public String[] getMessages() {
161: return delegate.getMessages();
162: }
163:
164: public Throwable getThrowable(int index) {
165: return delegate.getThrowable(index);
166: }
167:
168: public int getThrowableCount() {
169: return delegate.getThrowableCount();
170: }
171:
172: public Throwable[] getThrowables() {
173: return delegate.getThrowables();
174: }
175:
176: public int indexOfThrowable(Class type) {
177: return delegate.indexOfThrowable(type, 0);
178: }
179:
180: public int indexOfThrowable(Class type, int fromIndex) {
181: return delegate.indexOfThrowable(type, fromIndex);
182: }
183:
184: public void printStackTrace() {
185: delegate.printStackTrace();
186: }
187:
188: public void printStackTrace(PrintStream out) {
189: delegate.printStackTrace(out);
190: }
191:
192: public void printStackTrace(PrintWriter out) {
193: delegate.printStackTrace(out);
194: }
195:
196: public final void printPartialStackTrace(PrintWriter out) {
197: super .printStackTrace(out);
198: }
199:
200: private void writeObject(ObjectOutputStream oos) throws IOException {
201: Throwable tempCause = cause;
202: //don't propagate RecognitionException, might be not serializable
203: if (cause instanceof RecognitionException) {
204: cause = null;
205: }
206: oos.defaultWriteObject();
207: cause = tempCause;
208: }
209:
210: }
|