KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > MathException


1 /*
2  * Copyright 2003-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.math;
17
18 import java.io.PrintStream JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20
21
22 /**
23 * Base class for commons-math checked exceptions.
24 * <p>
25 * Supports nesting, emulating JDK 1.4 behavior if necessary.
26 * <p>
27 * Adapted from {@link org.apache.commons.collections.FunctorException}.
28 *
29 * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
30 */

31 public class MathException extends Exception JavaDoc {
32     
33     /** Serializable version identifier */
34     static final long serialVersionUID = -8594613561393443827L;
35     
36     /**
37      * Does JDK support nested exceptions?
38      */

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

55     private final Throwable JavaDoc rootCause;
56     
57     /**
58      * Constructs a new <code>MathException</code> with no
59      * detail message.
60      */

61     public MathException() {
62         super();
63         this.rootCause = null;
64     }
65     
66     /**
67      * Constructs a new <code>MathException</code> with specified
68      * detail message.
69      *
70      * @param msg the error message.
71      */

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

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

97     public MathException(String JavaDoc msg, Throwable JavaDoc rootCause) {
98         super(msg);
99         this.rootCause = rootCause;
100     }
101     
102     /**
103      * Gets the cause of this throwable.
104      *
105      * @return the cause of this throwable, or <code>null</code>
106      */

107     public Throwable JavaDoc getCause() {
108         return rootCause;
109     }
110     
111     /**
112      * Prints the stack trace of this exception to the standard error stream.
113      */

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

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

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