KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > StackableException


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------------
28  * StackableException.java
29  * -----------------------
30  * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: StackableException.java,v 1.3 2005/11/14 10:56:55 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 06-Dec-2002 : Initial version
40  * 10-Dec-2002 : Fixed issues reported by Checkstyle (DG);
41  * 29-Apr-2003 : Distilled from the JFreeReport project and moved into JCommon
42  *
43  */

44
45 package org.jfree.util;
46
47 import java.io.PrintStream JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49
50 /**
51  * A baseclass for exceptions, which could have parent exceptions. These parent exceptions
52  * are raised in a subclass and are now wrapped into a subclass of this Exception.
53  * <p>
54  * The parents are printed when this exception is printed. This class exists mainly for
55  * debugging reasons, as with them it is easier to detect the root cause of an error.
56  *
57  * <!-- In a perfect world there would be no need for such a class :)-->
58  *
59  * @author Thomas Morgner
60  */

61 public abstract class StackableException extends Exception JavaDoc {
62
63     /** The parent exception. */
64     private Exception JavaDoc parent;
65
66     /**
67      * Creates a StackableRuntimeException with no message and no parent.
68      */

69     public StackableException() {
70         super();
71     }
72
73     /**
74      * Creates an exception.
75      *
76      * @param message the exception message.
77      * @param ex the parent exception.
78      */

79     public StackableException(final String JavaDoc message, final Exception JavaDoc ex) {
80         super(message);
81         this.parent = ex;
82     }
83
84     /**
85      * Creates an exception.
86      *
87      * @param message the exception message.
88      */

89     public StackableException(final String JavaDoc message) {
90         super(message);
91     }
92
93     /**
94      * Returns the parent exception (possibly null).
95      *
96      * @return the parent exception.
97      */

98     public Exception JavaDoc getParent() {
99         return this.parent;
100     }
101
102     /**
103      * Prints the stack trace to the specified stream.
104      *
105      * @param stream the output stream.
106      */

107     public void printStackTrace(final PrintStream JavaDoc stream) {
108         super.printStackTrace(stream);
109         if (getParent() != null) {
110             stream.println("ParentException: ");
111             getParent().printStackTrace(stream);
112         }
113     }
114
115     /**
116      * Prints the stack trace to the specified writer.
117      *
118      * @param writer the writer.
119      */

120     public void printStackTrace(final PrintWriter JavaDoc writer) {
121         super.printStackTrace(writer);
122         if (getParent() != null) {
123             writer.println("ParentException: ");
124             getParent().printStackTrace(writer);
125         }
126     }
127
128     /**
129      * Prints this <code>Throwable</code> and its backtrace to the
130      * standard error stream. This method prints a stack trace for this
131      * <code>Throwable</code> object on the error output stream that is
132      * the value of the field <code>System.err</code>. The first line of
133      * output contains the result of the {@link #toString()} method for
134      * this object. Remaining lines represent data previously recorded by
135      * the method {@link #fillInStackTrace()}. The format of this
136      * information depends on the implementation, but the following
137      * example may be regarded as typical:
138      * <blockquote><pre>
139      * java.lang.NullPointerException
140      * at MyClass.mash(MyClass.java:9)
141      * at MyClass.crunch(MyClass.java:6)
142      * at MyClass.main(MyClass.java:3)
143      * </pre></blockquote>
144      * This example was produced by running the program:
145      * <blockquote><pre>
146      *
147      * class MyClass {
148      *
149      * public static void main(String[] argv) {
150      * crunch(null);
151      * }
152      * static void crunch(int[] a) {
153      * mash(a);
154      * }
155      *
156      * static void mash(int[] b) {
157      * System.out.println(b[0]);
158      * }
159      * }
160      * </pre></blockquote>
161      *
162      * @see System#err
163      */

164     public void printStackTrace() {
165         synchronized (System.err) {
166             printStackTrace(System.err);
167         }
168     }
169 }
170
Popular Tags