KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > exception > NestableRuntimeException


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.exception;
17
18 import java.io.PrintStream JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20
21 /**
22  * The base class of all runtime exceptions which can contain other
23  * exceptions.
24  *
25  * @see org.apache.commons.lang.exception.NestableException
26  * @author <a HREF="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
27  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
28  * @author <a HREF="mailto:knielsen@apache.org">Kasper Nielsen</a>
29  * @author <a HREF="mailto:steven@caswell.name">Steven Caswell</a>
30  * @since 1.0
31  * @version $Id: NestableRuntimeException.java 161243 2005-04-14 04:30:28Z ggregory $
32  */

33 public class NestableRuntimeException extends RuntimeException JavaDoc implements Nestable {
34     
35     /**
36      * The helper instance which contains much of the code which we
37      * delegate to.
38      */

39     protected NestableDelegate delegate = new NestableDelegate(this);
40
41     /**
42      * Holds the reference to the exception or error that caused
43      * this exception to be thrown.
44      */

45     private Throwable JavaDoc cause = null;
46
47     /**
48      * Constructs a new <code>NestableRuntimeException</code> without specified
49      * detail message.
50      */

51     public NestableRuntimeException() {
52         super();
53     }
54
55     /**
56      * Constructs a new <code>NestableRuntimeException</code> with specified
57      * detail message.
58      *
59      * @param msg the error message
60      */

61     public NestableRuntimeException(String JavaDoc msg) {
62         super(msg);
63     }
64
65     /**
66      * Constructs a new <code>NestableRuntimeException</code> with specified
67      * nested <code>Throwable</code>.
68      *
69      * @param cause the exception or error that caused this exception to be
70      * thrown
71      */

72     public NestableRuntimeException(Throwable JavaDoc cause) {
73         super();
74         this.cause = cause;
75     }
76
77     /**
78      * Constructs a new <code>NestableRuntimeException</code> with specified
79      * detail message and nested <code>Throwable</code>.
80      *
81      * @param msg the error message
82      * @param cause the exception or error that caused this exception to be
83      * thrown
84      */

85     public NestableRuntimeException(String JavaDoc msg, Throwable JavaDoc cause) {
86         super(msg);
87         this.cause = cause;
88     }
89
90     public Throwable JavaDoc getCause() {
91         return cause;
92     }
93
94     /**
95      * Returns the detail message string of this throwable. If it was
96      * created with a null message, returns the following:
97      * (cause==null ? null : cause.toString()).
98      *
99      * @return String message string of the throwable
100      */

101     public String JavaDoc getMessage() {
102         if (super.getMessage() != null) {
103             return super.getMessage();
104         } else if (cause != null) {
105             return cause.toString();
106         } else {
107             return null;
108         }
109     }
110
111     public String JavaDoc getMessage(int index) {
112         if (index == 0) {
113             return super.getMessage();
114         } else {
115             return delegate.getMessage(index);
116         }
117     }
118
119     public String JavaDoc[] getMessages() {
120         return delegate.getMessages();
121     }
122
123     public Throwable JavaDoc getThrowable(int index) {
124         return delegate.getThrowable(index);
125     }
126
127     public int getThrowableCount() {
128         return delegate.getThrowableCount();
129     }
130
131     public Throwable JavaDoc[] getThrowables() {
132         return delegate.getThrowables();
133     }
134
135     public int indexOfThrowable(Class JavaDoc type) {
136         return delegate.indexOfThrowable(type, 0);
137     }
138
139     public int indexOfThrowable(Class JavaDoc type, int fromIndex) {
140         return delegate.indexOfThrowable(type, fromIndex);
141     }
142
143     public void printStackTrace() {
144         delegate.printStackTrace();
145     }
146
147     public void printStackTrace(PrintStream JavaDoc out) {
148         delegate.printStackTrace(out);
149     }
150
151     public void printStackTrace(PrintWriter JavaDoc out) {
152         delegate.printStackTrace(out);
153     }
154
155     public final void printPartialStackTrace(PrintWriter JavaDoc out) {
156         super.printStackTrace(out);
157     }
158
159 }
160
Popular Tags