KickJava   Java API By Example, From Geeks To Geeks.

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


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 errors which can contain other exceptions.
23  *
24  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
25  * @see org.apache.commons.lang.exception.NestableException
26  * @since 1.0
27  * @version $Id: NestableError.java 161243 2005-04-14 04:30:28Z ggregory $
28  */

29 public class NestableError extends Error JavaDoc implements Nestable {
30
31     /**
32      * The helper instance which contains much of the code which we
33      * delegate to.
34      */

35     protected NestableDelegate delegate = new NestableDelegate(this);
36
37     /**
38      * Holds the reference to the exception or error that caused
39      * this exception to be thrown.
40      */

41     private Throwable JavaDoc cause = null;
42
43     /**
44      * Constructs a new <code>NestableError</code> without specified
45      * detail message.
46      */

47     public NestableError() {
48         super();
49     }
50
51     /**
52      * Constructs a new <code>NestableError</code> with specified
53      * detail message.
54      *
55      * @param msg The error message.
56      */

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

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

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

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