View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.foundation.internal;
20  
21  import java.io.PrintStream;
22  import java.io.PrintWriter;
23  
24  /***
25  * This is a simple RuntimeException that can encapsulate
26  * another throwable.  Behaves as a normal RuntimeException
27  * except that it prints a stack trace of the wrapped 
28  * throwable if one is specified.
29  *
30  * Intended to be used anytime you'd 
31  * report an exception with System.out.println: that is,
32  * for debugging and non-user-visible exceptions.
33  *
34  * @author michael@mpowers.net
35  * @author $Author: cgruber $
36  * @version $Revision: 893 $
37  */
38  
39  public class WotonomyException extends RuntimeException
40  {
41  	protected String message;
42  	protected Throwable wrappedThrowable;
43  	
44  	/***
45  	* Default constructor.
46  	*/ 
47  	public WotonomyException()
48  	{
49  		super();
50  		message = null;
51  		wrappedThrowable = null;	
52  	}
53  	
54  	/***
55  	* Standard constructor with message.
56  	*/
57  	public WotonomyException( String aMessage )
58  	{
59  		super( aMessage );
60  		message = aMessage;
61  		wrappedThrowable = null;
62  	}
63  	
64  	/***
65  	* Specifies a throwable to wrap.
66  	*/ 
67  	public WotonomyException( Throwable aThrowable )
68  	{
69  		super();
70  		message = null;
71  		wrappedThrowable = aThrowable;
72  	}
73  	
74  	/***
75  	* Specifies a message and a throwable to wrap.
76  	*/ 
77  	public WotonomyException( String aMessage, Throwable aThrowable )
78  	{
79  		super( aMessage );
80  		message = aMessage;
81  		wrappedThrowable = aThrowable;
82  	}
83      
84      /***
85      * Returns the wrapped throwable.
86      */
87      public Throwable getWrappedThrowable()
88      {
89          return wrappedThrowable;   
90      }
91  	
92  	public void printStackTrace(PrintWriter s)
93  	{
94  		if ( message != null )
95  		{
96  			s.println( "Exception: " + message );	
97  		}
98  		if ( wrappedThrowable != null )
99  		{
100 			wrappedThrowable.printStackTrace( s );
101 			return;
102 		}
103 		super.printStackTrace( s );
104 	}
105 	
106 	public void printStackTrace(PrintStream s)
107 	{
108 		if ( message != null )
109 		{
110 			s.println( "Exception: " + message );	
111 		}
112 		if ( wrappedThrowable != null )
113 		{
114 			wrappedThrowable.printStackTrace( s );	
115 			return;
116 		}
117 		super.printStackTrace( s );
118 	}
119 	
120 	public void printStackTrace()
121 	{
122 		if ( message != null )
123 		{
124 			System.err.println( "Exception: " + message );	
125 		}
126 		if ( wrappedThrowable != null )
127 		{
128 			wrappedThrowable.printStackTrace();	
129 			return;
130 		}
131 		super.printStackTrace();
132 	}
133 	
134 }