/* Wotonomy: OpenStep design patterns for pure Java applications. Copyright (C) 2000 Michael Powers This library 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 2.1 of the License, or (at your option) any later version. This library 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 this library; if not, see http://www.gnu.org */ package net.wotonomy.foundation; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; /** * Serves to wrap an exception inside of a RuntimeException, which is not * required to be declared in a throws statement. * * @author michael@mpowers.net * @author $Author: cgruber $ * @version $Revision: 893 $ */ public class NSForwardException extends RuntimeException { protected String message; protected Throwable wrappedThrowable; /** * Default constructor. */ public NSForwardException() { super(); message = null; wrappedThrowable = null; } /** * Standard constructor with message. */ public NSForwardException(String aMessage) { super(aMessage); message = aMessage; wrappedThrowable = null; } /** * Specifies a throwable to wrap. */ public NSForwardException(Throwable aThrowable) { super(); message = null; wrappedThrowable = aThrowable; } /** * Specifies a message and a throwable to wrap. */ public NSForwardException(Throwable aThrowable, String aMessage) { super(aMessage); message = aMessage; wrappedThrowable = aThrowable; } /** * Returns the wrapped throwable. */ public Throwable originalException() { return wrappedThrowable; } public void printStackTrace(PrintWriter s) { if (message != null) { s.println(toString()); } if (wrappedThrowable != null) { wrappedThrowable.printStackTrace(s); return; } super.printStackTrace(s); } public void printStackTrace(PrintStream s) { if (message != null) { s.println(toString()); } if (wrappedThrowable != null) { wrappedThrowable.printStackTrace(s); return; } super.printStackTrace(s); } public void printStackTrace() { if (message != null) { System.err.println(toString()); } if (wrappedThrowable != null) { wrappedThrowable.printStackTrace(); return; } super.printStackTrace(); } public String stackTrace() { StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); if (wrappedThrowable != null) { wrappedThrowable.printStackTrace(printWriter); } else { super.printStackTrace(printWriter); } printWriter.flush(); printWriter.close(); return writer.toString(); } public String toString() { String result = message; if (result == null) result = ""; if (wrappedThrowable != null) { result = wrappedThrowable.toString() + " : " + result; } return result; } }