org.jfree.util.StackableException Maven / Gradle / Ivy
Show all versions of jtstand-common Show documentation
/*
* Copyright (c) 2009 Albert Kurucz.
*
* This file, StackableException.java is part of JTStand.
*
* JTStand is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JTStand is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GTStand. If not, see .
*/
package org.jfree.util;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* A baseclass for exceptions, which could have parent exceptions. These parent exceptions
* are raised in a subclass and are now wrapped into a subclass of this Exception.
*
* The parents are printed when this exception is printed. This class exists mainly for
* debugging reasons, as with them it is easier to detect the root cause of an error.
*
*
*
* @author Thomas Morgner
*/
public abstract class StackableException extends Exception {
/** The parent exception. */
private Exception parent;
/**
* Creates a StackableRuntimeException with no message and no parent.
*/
public StackableException() {
super();
}
/**
* Creates an exception.
*
* @param message the exception message.
* @param ex the parent exception.
*/
public StackableException(final String message, final Exception ex) {
super(message);
this.parent = ex;
}
/**
* Creates an exception.
*
* @param message the exception message.
*/
public StackableException(final String message) {
super(message);
}
/**
* Returns the parent exception (possibly null).
*
* @return the parent exception.
*/
public Exception getParent() {
return this.parent;
}
/**
* Prints the stack trace to the specified stream.
*
* @param stream the output stream.
*/
public void printStackTrace(final PrintStream stream) {
super.printStackTrace(stream);
if (getParent() != null) {
stream.println("ParentException: ");
getParent().printStackTrace(stream);
}
}
/**
* Prints the stack trace to the specified writer.
*
* @param writer the writer.
*/
public void printStackTrace(final PrintWriter writer) {
super.printStackTrace(writer);
if (getParent() != null) {
writer.println("ParentException: ");
getParent().printStackTrace(writer);
}
}
/**
* Prints this Throwable
and its backtrace to the
* standard error stream. This method prints a stack trace for this
* Throwable
object on the error output stream that is
* the value of the field System.err
. The first line of
* output contains the result of the {@link #toString()} method for
* this object. Remaining lines represent data previously recorded by
* the method {@link #fillInStackTrace()}. The format of this
* information depends on the implementation, but the following
* example may be regarded as typical:
*
* java.lang.NullPointerException
* at MyClass.mash(MyClass.java:9)
* at MyClass.crunch(MyClass.java:6)
* at MyClass.main(MyClass.java:3)
*
* This example was produced by running the program:
*
*
* class MyClass {
*
* public static void main(String[] argv) {
* crunch(null);
* }
* static void crunch(int[] a) {
* mash(a);
* }
*
* static void mash(int[] b) {
* System.out.println(b[0]);
* }
* }
*
*
* @see System#err
*/
public void printStackTrace() {
synchronized (System.err) {
printStackTrace(System.err);
}
}
}