001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math;
017:
018: import java.io.PrintStream;
019: import java.io.PrintWriter;
020:
021: /**
022: * Base class for commons-math checked exceptions.
023: * <p>
024: * Supports nesting, emulating JDK 1.4 behavior if necessary.
025: * <p>
026: * Adapted from {@link org.apache.commons.collections.FunctorException}.
027: *
028: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
029: */
030: public class MathException extends Exception {
031:
032: /** Serializable version identifier */
033: private static final long serialVersionUID = -8594613561393443827L;
034:
035: /**
036: * Does JDK support nested exceptions?
037: */
038: private static final boolean JDK_SUPPORTS_NESTED;
039:
040: static {
041: boolean flag = false;
042: try {
043: Throwable.class.getDeclaredMethod("getCause", new Class[0]);
044: flag = true;
045: } catch (NoSuchMethodException ex) {
046: flag = false;
047: }
048: JDK_SUPPORTS_NESTED = flag;
049: }
050:
051: /**
052: * Root cause of the exception
053: */
054: private final Throwable rootCause;
055:
056: /**
057: * Constructs a new <code>MathException</code> with no
058: * detail message.
059: */
060: public MathException() {
061: super ();
062: this .rootCause = null;
063: }
064:
065: /**
066: * Constructs a new <code>MathException</code> with specified
067: * detail message.
068: *
069: * @param msg the error message.
070: */
071: public MathException(String msg) {
072: super (msg);
073: this .rootCause = null;
074: }
075:
076: /**
077: * Constructs a new <code>MathException</code> with specified
078: * nested <code>Throwable</code> root cause.
079: *
080: * @param rootCause the exception or error that caused this exception
081: * to be thrown.
082: */
083: public MathException(Throwable rootCause) {
084: super ((rootCause == null ? null : rootCause.getMessage()));
085: this .rootCause = rootCause;
086: }
087:
088: /**
089: * Constructs a new <code>MathException</code> with specified
090: * detail message and nested <code>Throwable</code> root cause.
091: *
092: * @param msg the error message.
093: * @param rootCause the exception or error that caused this exception
094: * to be thrown.
095: */
096: public MathException(String msg, Throwable rootCause) {
097: super (msg);
098: this .rootCause = rootCause;
099: }
100:
101: /**
102: * Gets the cause of this throwable.
103: *
104: * @return the cause of this throwable, or <code>null</code>
105: */
106: public Throwable getCause() {
107: return rootCause;
108: }
109:
110: /**
111: * Prints the stack trace of this exception to the standard error stream.
112: */
113: public void printStackTrace() {
114: printStackTrace(System.err);
115: }
116:
117: /**
118: * Prints the stack trace of this exception to the specified stream.
119: *
120: * @param out the <code>PrintStream</code> to use for output
121: */
122: public void printStackTrace(PrintStream out) {
123: synchronized (out) {
124: PrintWriter pw = new PrintWriter(out, false);
125: printStackTrace(pw);
126: // Flush the PrintWriter before it's GC'ed.
127: pw.flush();
128: }
129: }
130:
131: /**
132: * Prints the stack trace of this exception to the specified writer.
133: *
134: * @param out the <code>PrintWriter</code> to use for output
135: */
136: public void printStackTrace(PrintWriter out) {
137: synchronized (out) {
138: super .printStackTrace(out);
139: if (rootCause != null && JDK_SUPPORTS_NESTED == false) {
140: out.print("Caused by: ");
141: rootCause.printStackTrace(out);
142: }
143: }
144: }
145:
146: }
|