View Javadoc

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;
19  import java.io.PrintWriter;
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: 1.20 $ $Date: 2004/11/07 02:17:56 $
30  */
31  public class MathException extends Exception {
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.class.getDeclaredMethod("getCause", new Class[0]);
45              flag = true;
46          } catch (NoSuchMethodException ex) {
47              flag = false;
48          }
49          JDK_SUPPORTS_NESTED = flag;
50      }
51      
52      /***
53       * Root cause of the exception
54       */
55      private final Throwable 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 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 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 msg, Throwable 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 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 out) {
124         synchronized (out) {
125             PrintWriter pw = new PrintWriter(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 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 }