1   package attrib4j;
2   
3   import java.io.PrintStream;
4   import java.io.PrintWriter;
5   import java.io.StringWriter;
6   
7   /***
8    * It allows you nest exceptions.  This results in simplifing the 
9    * external API of a package or library.  
10   *
11   * Implementation inspired from various placed, most recently
12   * Javaworld, Javatips #91.  Now included as part of J2SE 1.4
13   * 
14   * @author <a href="mailto:mpollack@speakeasy.net">Mark Pollack</a>
15   * @version $Revision: 1.4 $ $Date: 2003/03/22 17:56:20 $
16   */
17  public class AttributeException extends Exception {
18  
19      /*
20       * By giving <code>AttributeException</code> a reference to a 
21       * Throwable object, exception chaining can be enforced easily.
22       */
23      private Throwable _previousThrowable = null;
24  
25      /***
26       * The yucky stack trace string.
27       *
28       */
29      private String _stackTraceString;
30  
31      /***
32       * Do nothing constructor.
33       *
34       */
35      public AttributeException() {
36      }
37  
38      /***
39       * Creates a new <code>AttributeException</code> instance.  
40       * @param message a <code>String</code> value
41       */
42      public AttributeException(String message) {
43          super(message);
44      }
45  
46      /***
47       * Create an exception nesting the rootCause of this exception.
48       *
49       * @param rootCause a value of type 'Throwable'.
50       */
51      public AttributeException(Throwable rootCause) {
52          super();
53          if (rootCause != null) {
54              _previousThrowable = rootCause;
55              _stackTraceString = generateStackTraceString(rootCause);
56          } else {
57              _previousThrowable =
58                  new Exception(
59                      "ERROR: A null value for rootCause was passed "
60                          + "to the constructor of AttributeException");
61              _stackTraceString = generateStackTraceString(_previousThrowable);
62          }
63      }
64  
65      /***
66       * Create a new <code>AttributeException</code> instance.  Allows for 
67       * an association of an originating exception.
68       *
69       * @param message Message of the exception.
70       * message of the exception.
71       * @param rootCause The exception that caused this exception to be raised.
72       */
73      public AttributeException(String message, Throwable rootCause) {
74          super(message);
75          if (rootCause != null) {
76              _previousThrowable = rootCause;
77              _stackTraceString = generateStackTraceString(rootCause);
78          } else {
79              _previousThrowable =
80                  new Exception(
81                      "ERROR: A null value for rootCause was passed "
82                          + "to the constructor of AttributeException");
83              _stackTraceString = generateStackTraceString(_previousThrowable);
84          }
85      }
86  
87      /***
88       * Get the exception that caused this exception to be raised.
89       *
90       * @return the root exception.
91       */
92      public Throwable getRootCause() {
93          return _previousThrowable;
94      }
95  
96      /***
97       * Return the stack trace including root cause exceptions.
98       *
99       * @return The stack trace of the exception.
100      */
101     public String getStackTraceString() {
102         // if there's no nested exception, there's no stackTrace
103         if (_previousThrowable == null) {
104             return null;
105         }
106 
107         StringBuffer traceBuffer = new StringBuffer();
108 
109         if (_previousThrowable instanceof AttributeException) {
110 
111             traceBuffer.append(
112                 ((AttributeException) _previousThrowable)
113                     .getStackTraceString());
114             traceBuffer.append("-------- nested by:\n");
115         }
116 
117         traceBuffer.append(_stackTraceString);
118         return traceBuffer.toString();
119     }
120 
121     /***
122      * Override Exceptipn.getMessage to include information of the root
123      * cause of the exception.
124      *
125      * @return The exception message, including root cause messages.
126      */
127     public String getMessage() {
128         // superMsg will contain whatever String was passed into the
129         // constructor, and null otherwise.
130         String superMsg = super.getMessage();
131 
132         // if there's no nested exception, do like we would always do
133         if (getRootCause() == null) {
134             return superMsg;
135         }
136 
137         StringBuffer theMsg = new StringBuffer();
138 
139         // get the nested exception's message
140         String nestedMsg = getRootCause().getMessage();
141 
142         if (superMsg != null) {
143             theMsg.append(superMsg).append(": ").append(nestedMsg);
144         } else {
145             theMsg.append(nestedMsg);
146         }
147 
148         return theMsg.toString();
149     }
150 
151     /* 
152      * Print out the nested exception.
153      * @see java.lang.Object#toString()
154      */
155     public String toString() {
156         StringBuffer theMsg = new StringBuffer(super.toString());
157 
158         if (getRootCause() != null) {
159             theMsg.append("; \n\t---> nested ").append(getRootCause());
160         }
161 
162         return theMsg.toString();
163     }
164 
165     /***
166      * Override default behavior
167      *
168      */
169     public void printStackTrace() {
170         super.printStackTrace();
171         if (_previousThrowable != null) {
172             _previousThrowable.printStackTrace();
173         }
174     }
175 
176     /***
177      * Override default behavior
178      *
179      */
180     public void printStackTrace(PrintStream inPrintStream) {
181         super.printStackTrace(inPrintStream);
182         if (_previousThrowable != null) {
183             _previousThrowable.printStackTrace(inPrintStream);
184         }
185     }
186 
187     /***
188      * Override default behavior
189      *
190      */
191     public void printStackTrace(PrintWriter inPrintWriter) {
192         super.printStackTrace(inPrintWriter);
193         if (_previousThrowable != null) {
194             _previousThrowable.printStackTrace(inPrintWriter);
195         }
196     }
197 
198     /***
199      * Generate the StackTraceString
200      *
201      * @param t a <code>Throwable</code> value
202      * @return a <code>String</code> value
203      */
204     private static String generateStackTraceString(Throwable t) {
205         StringWriter s = new StringWriter();
206         t.printStackTrace(new PrintWriter(s));
207         return s.toString();
208 
209     }
210 
211 }
This page was automatically generated by Maven