KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > FunctorException


1 /*
2  * Copyright 2001-2004 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.collections;
17
18 import java.io.PrintStream JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20
21 /**
22  * Runtime exception thrown from functors.
23  * If required, a root cause error can be wrapped within this one.
24  *
25  * @since Commons Collections 3.0
26  * @version $Revision: 1.6 $ $Date: 2004/02/18 01:15:42 $
27  *
28  * @author Stephen Colebourne
29  */

30 public class FunctorException extends RuntimeException JavaDoc {
31     
32     /**
33      * Does JDK support nested exceptions
34      */

35     private static final boolean JDK_SUPPORTS_NESTED;
36     
37     static {
38         boolean flag = false;
39         try {
40             Throwable JavaDoc.class.getDeclaredMethod("getCause", new Class JavaDoc[0]);
41             flag = true;
42         } catch (NoSuchMethodException JavaDoc ex) {
43             flag = false;
44         }
45         JDK_SUPPORTS_NESTED = flag;
46     }
47     
48     /**
49      * Root cause of the exception
50      */

51     private final Throwable JavaDoc rootCause;
52
53     /**
54      * Constructs a new <code>FunctorException</code> without specified
55      * detail message.
56      */

57     public FunctorException() {
58         super();
59         this.rootCause = null;
60     }
61
62     /**
63      * Constructs a new <code>FunctorException</code> with specified
64      * detail message.
65      *
66      * @param msg the error message.
67      */

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

80     public FunctorException(Throwable JavaDoc rootCause) {
81         super((rootCause == null ? null : rootCause.getMessage()));
82         this.rootCause = rootCause;
83     }
84
85     /**
86      * Constructs a new <code>FunctorException</code> with specified
87      * detail message and nested <code>Throwable</code> root cause.
88      *
89      * @param msg the error message.
90      * @param rootCause the exception or error that caused this exception
91      * to be thrown.
92      */

93     public FunctorException(String JavaDoc msg, Throwable JavaDoc rootCause) {
94         super(msg);
95         this.rootCause = rootCause;
96     }
97
98     /**
99      * Gets the cause of this throwable.
100      *
101      * @return the cause of this throwable, or <code>null</code>
102      */

103     public Throwable JavaDoc getCause() {
104         return rootCause;
105     }
106
107     /**
108      * Prints the stack trace of this exception to the standard error stream.
109      */

110     public void printStackTrace() {
111         printStackTrace(System.err);
112     }
113
114     /**
115      * Prints the stack trace of this exception to the specified stream.
116      *
117      * @param out the <code>PrintStream</code> to use for output
118      */

119     public void printStackTrace(PrintStream JavaDoc out) {
120         synchronized (out) {
121             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(out, false);
122             printStackTrace(pw);
123             // Flush the PrintWriter before it's GC'ed.
124
pw.flush();
125         }
126     }
127
128     /**
129      * Prints the stack trace of this exception to the specified writer.
130      *
131      * @param out the <code>PrintWriter</code> to use for output
132      */

133     public void printStackTrace(PrintWriter JavaDoc out) {
134         synchronized (out) {
135             super.printStackTrace(out);
136             if (rootCause != null && JDK_SUPPORTS_NESTED == false) {
137                 out.print("Caused by: ");
138                 rootCause.printStackTrace(out);
139             }
140         }
141     }
142
143 }
144
Popular Tags