Source code: com/thermidor/util/exception/GenericRuntimeException.java
1 package com.thermidor.util.exception;
2 import java.io.PrintWriter;
3 import java.io.PrintStream;
4 /*@LEGAL@*/
5 /**
6 * The purpose of the GenericRuntimeException is to provied a base class for
7 * exceptions. This Exception class is similar to the new version of the
8 * Throwable class provided in version 1.4 of the J2SDK in that it provides
9 * for nested exceptions.
10 * @version 1.0
11 * @author Edward Turnock
12 */
13 public abstract class GenericRuntimeException extends RuntimeException {
14
15 /**
16 * The root cause of this exception instance.
17 */
18 private Throwable nested;
19
20 /**
21 * A message constant to indicate the state of the nested exception
22 * stack trace.
23 */
24 private static final String START = "[[NESTED>>";
25
26 /**
27 * A message constant to indicate the end of the message past of the nested
28 * exception.
29 */
30 private static final String START_END = "<<]";
31
32 /**
33 * A message constant to indicate the end of the stack trace of the nested
34 * exception.
35 */
36 private static final String END = "<<NESTED]";
37
38 /**
39 * Print the stack trace of this exception and its nested exception instance
40 * to standard error.
41 */
42 public void printStackTrace() {
43 super.printStackTrace();
44
45 if (nested != null) {
46 System.err.println(START + nested.getMessage() + START_END);
47 nested.printStackTrace(System.err);
48 System.err.println(END);
49 }
50 }
51
52 /**
53 * Print the statck trace of this exception and the nested Throwable
54 * instance to the specified print writer.
55 * @param pw the print writer to print the stack trace to.
56 */
57 public void printStackTrace(PrintWriter pw) {
58 super.printStackTrace(pw);
59
60 if (nested != null) {
61 pw.println(START + nested.getMessage() + START_END);
62 nested.printStackTrace(pw);
63 pw.println(END);
64 }
65 }
66
67 /**
68 * Print the stack trace of this exception and the nested Throwable
69 * instance to the specified print stream.
70 * @param ps the print stream to print the stack trace to.
71 */
72 public void printStackTrace(PrintStream ps) {
73 super.printStackTrace(ps);
74
75 if (nested != null) {
76 ps.println(START + nested.getMessage() + START_END);
77 nested.printStackTrace(ps);
78 ps.println(END);
79 }
80 }
81
82 /**
83 * Retrieve the Throwable instance that was the root cause of this exception
84 * or null if there was no root cause.
85 * @return the Throwable instance that was the root cause of this exception.
86 */
87 public Throwable getRootCause() {
88 return nested;
89 }
90
91 /**
92 * Construct a default instance of the GenericRuntimeException class.
93 */
94 public GenericRuntimeException() {
95 super();
96 }
97
98 /**
99 * Construct a new instance of the GenericRuntimeException with the
100 * specified message string.
101 * @param message the message string with which to construct this instance.
102 */
103 public GenericRuntimeException(String message) {
104 super(message);
105 }
106
107 /**
108 * Construct an instance of a GenericRuntimeException that encapsulates the
109 * specified Throwable instance as the root cause.
110 * @param t the root cause of this exception being constructed.
111 */
112 public GenericRuntimeException(Throwable t) {
113 super();
114 nested = t;
115 }
116
117 /**
118 * Construct a new instance of a GenericRuntimeException with the specified
119 * root cause and message string.
120 * @param t the root cause of the exception.
121 * @param message the descriptive string for this instance.
122 */
123 public GenericRuntimeException(Throwable t, String message) {
124 super(message);
125 nested = t;
126 }
127 }
128
129
130
131
132