001    /**
002     * www.jcoverage.com
003     * Copyright (C)2003 jcoverage ltd.
004     *
005     * This file is part of jcoverage.
006     *
007     * jcoverage is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published
009     * by the Free Software Foundation; either version 2 of the License,
010     * or (at your option) any later version.
011     *
012     * jcoverage is distributed in the hope that it will be useful, but
013     * WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with jcoverage; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020     * USA
021     *
022     */
023    package com.jcoverage.reporting;
024    
025    import java.io.PrintStream;
026    import java.io.PrintWriter;
027    
028    /**
029     * A nestable exception supporting exception chaining.
030     */
031    public class ReportingException extends Exception {
032    
033      private Throwable cause;
034        
035      /**
036       * Create the exception with just a message. Only use this
037       * constructor if there is no cause that can be attributed.
038       */
039      public ReportingException(String s) {
040        super(s);
041      }
042        
043      /**
044       * Create the exception with a message and cause.
045       */
046      public ReportingException(String s,Throwable cause) {
047        super(s);
048        this.cause = cause;
049      }
050    
051      public void printStackTrace() { 
052        super.printStackTrace();
053        if (cause!=null) {
054          System.err.println("Caused by: ");
055          cause.printStackTrace();
056        }
057      }
058    
059      public void printStackTrace(PrintStream out) { 
060        super.printStackTrace(out);
061        if (cause!=null) {
062          out.println("Caused by: ");
063          cause.printStackTrace(out);
064        }
065      }
066      
067      public void printStackTrace(PrintWriter out) { 
068        super.printStackTrace(out);
069        if (cause!=null) {
070          out.println("Caused by: ");
071          cause.printStackTrace(out);
072        }
073      }
074    
075      public String getMessage() {
076        StringBuffer sb=new StringBuffer(super.getMessage());
077        if (cause!=null) {
078          sb.append("\nCaused by:\n");
079          sb.append(cause.toString());
080        }
081        return sb.toString();
082      }
083    }