001: /*
002: * Copyright 2001-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.collections;
017:
018: import java.io.PrintStream;
019: import java.io.PrintWriter;
020:
021: /**
022: * Runtime exception thrown from functors.
023: * If required, a root cause error can be wrapped within this one.
024: *
025: * @since Commons Collections 3.0
026: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
027: *
028: * @author Stephen Colebourne
029: */
030: public class FunctorException extends RuntimeException {
031:
032: /**
033: * Does JDK support nested exceptions
034: */
035: private static final boolean JDK_SUPPORTS_NESTED;
036:
037: static {
038: boolean flag = false;
039: try {
040: Throwable.class.getDeclaredMethod("getCause", new Class[0]);
041: flag = true;
042: } catch (NoSuchMethodException ex) {
043: flag = false;
044: }
045: JDK_SUPPORTS_NESTED = flag;
046: }
047:
048: /**
049: * Root cause of the exception
050: */
051: private final Throwable rootCause;
052:
053: /**
054: * Constructs a new <code>FunctorException</code> without specified
055: * detail message.
056: */
057: public FunctorException() {
058: super ();
059: this .rootCause = null;
060: }
061:
062: /**
063: * Constructs a new <code>FunctorException</code> with specified
064: * detail message.
065: *
066: * @param msg the error message.
067: */
068: public FunctorException(String msg) {
069: super (msg);
070: this .rootCause = null;
071: }
072:
073: /**
074: * Constructs a new <code>FunctorException</code> with specified
075: * nested <code>Throwable</code> root cause.
076: *
077: * @param rootCause the exception or error that caused this exception
078: * to be thrown.
079: */
080: public FunctorException(Throwable rootCause) {
081: super ((rootCause == null ? null : rootCause.getMessage()));
082: this .rootCause = rootCause;
083: }
084:
085: /**
086: * Constructs a new <code>FunctorException</code> with specified
087: * detail message and nested <code>Throwable</code> root cause.
088: *
089: * @param msg the error message.
090: * @param rootCause the exception or error that caused this exception
091: * to be thrown.
092: */
093: public FunctorException(String msg, Throwable rootCause) {
094: super (msg);
095: this .rootCause = rootCause;
096: }
097:
098: /**
099: * Gets the cause of this throwable.
100: *
101: * @return the cause of this throwable, or <code>null</code>
102: */
103: public Throwable 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 out) {
120: synchronized (out) {
121: PrintWriter pw = new PrintWriter(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 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: }
|