Source for org.jfree.util.StackableRuntimeException

   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:  * StackableRuntimeException.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: StackableRuntimeException.java,v 1.3 2005/11/14 10:57:07 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;
  48: import java.io.PrintWriter;
  49: 
  50: /**
  51:  * A baseclass for RuntimeExceptions, 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:  * @author Thomas Morgner
  58:  */
  59: public class StackableRuntimeException extends RuntimeException {
  60: 
  61:     /** The parent exception. */
  62:     private Exception parent;
  63: 
  64:     /**
  65:      * Creates a StackableRuntimeException with no message and no parent.
  66:      */
  67:     public StackableRuntimeException() {
  68:         super();
  69:     }
  70: 
  71:     /**
  72:      * Creates an exception.
  73:      *
  74:      * @param message  the exception message.
  75:      * @param ex  the parent exception.
  76:      */
  77:     public StackableRuntimeException(final String message, final Exception ex) {
  78:         super(message);
  79:         this.parent = ex;
  80:     }
  81: 
  82:     /**
  83:      * Creates an exception.
  84:      *
  85:      * @param message  the exception message.
  86:      */
  87:     public StackableRuntimeException(final String message) {
  88:         super(message);
  89:     }
  90: 
  91:     /**
  92:      * Returns the parent exception (possibly null).
  93:      *
  94:      * @return the parent exception.
  95:      */
  96:     public Exception getParent() {
  97:         return this.parent;
  98:     }
  99: 
 100:     /**
 101:      * Prints the stack trace to the specified stream.
 102:      *
 103:      * @param stream  the output stream.
 104:      */
 105:     public void printStackTrace(final PrintStream stream) {
 106:         super.printStackTrace(stream);
 107:         if (getParent() != null) {
 108:             stream.println("ParentException: ");
 109:             getParent().printStackTrace(stream);
 110:         }
 111:     }
 112: 
 113:     /**
 114:      * Prints the stack trace to the specified writer.
 115:      *
 116:      * @param writer  the writer.
 117:      */
 118:     public void printStackTrace(final PrintWriter writer) {
 119:         super.printStackTrace(writer);
 120:         if (getParent() != null) {
 121:             writer.println("ParentException: ");
 122:             getParent().printStackTrace(writer);
 123:         }
 124:     }
 125: 
 126: }