1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.foundation;
20
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24
25 /***
26 * Serves to wrap an exception inside of a RuntimeException,
27 * which is not required to be declared in a throws statement.
28 *
29 * @author michael@mpowers.net
30 * @author $Author: cgruber $
31 * @version $Revision: 893 $
32 */
33
34 public class NSForwardException extends RuntimeException
35 {
36 protected String message;
37 protected Throwable wrappedThrowable;
38
39 /***
40 * Default constructor.
41 */
42 public NSForwardException()
43 {
44 super();
45 message = null;
46 wrappedThrowable = null;
47 }
48
49 /***
50 * Standard constructor with message.
51 */
52 public NSForwardException( String aMessage )
53 {
54 super( aMessage );
55 message = aMessage;
56 wrappedThrowable = null;
57 }
58
59 /***
60 * Specifies a throwable to wrap.
61 */
62 public NSForwardException( Throwable aThrowable )
63 {
64 super();
65 message = null;
66 wrappedThrowable = aThrowable;
67 }
68
69 /***
70 * Specifies a message and a throwable to wrap.
71 */
72 public NSForwardException( Throwable aThrowable, String aMessage )
73 {
74 super( aMessage );
75 message = aMessage;
76 wrappedThrowable = aThrowable;
77 }
78
79 /***
80 * Returns the wrapped throwable.
81 */
82 public Throwable originalException()
83 {
84 return wrappedThrowable;
85 }
86
87 public void printStackTrace(PrintWriter s)
88 {
89 if ( message != null )
90 {
91 s.println( toString() );
92 }
93 if ( wrappedThrowable != null )
94 {
95 wrappedThrowable.printStackTrace( s );
96 return;
97 }
98 super.printStackTrace( s );
99 }
100
101 public void printStackTrace(PrintStream s)
102 {
103 if ( message != null )
104 {
105 s.println( toString() );
106 }
107 if ( wrappedThrowable != null )
108 {
109 wrappedThrowable.printStackTrace( s );
110 return;
111 }
112 super.printStackTrace( s );
113 }
114
115 public void printStackTrace()
116 {
117 if ( message != null )
118 {
119 System.err.println( toString() );
120 }
121 if ( wrappedThrowable != null )
122 {
123 wrappedThrowable.printStackTrace();
124 return;
125 }
126 super.printStackTrace();
127 }
128
129 public String stackTrace()
130 {
131 StringWriter writer = new StringWriter();
132 PrintWriter printWriter = new PrintWriter( writer );
133 if ( wrappedThrowable != null )
134 {
135 wrappedThrowable.printStackTrace( printWriter );
136 }
137 else
138 {
139 super.printStackTrace( printWriter );
140 }
141 printWriter.flush();
142 printWriter.close();
143 return writer.toString();
144 }
145
146 public String toString()
147 {
148 String result = message;
149 if ( result == null ) result = "";
150 if ( wrappedThrowable != null )
151 {
152 result = wrappedThrowable.toString() + " : " + result;
153 }
154 return result;
155 }
156
157 }