View Javadoc

1   /*
2    * Copyright 2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.scaffold.lang;
18  
19  import org.apache.commons.scaffold.text.ConvertUtils;
20  
21  
22      /**
23       * Mimicks new functionality in 1.4
24       * http://www.javaworld.com/javaworld/jw-08-2001/jw-0803-exceptions.html
25       * @author Brian Geotz
26       * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
27       */
28      public class ChainedException extends Exception {
29  
30           private static final String CAUSED_BY = "Caused by: ";
31  
32           private Throwable cause;
33  
34           public ChainedException() {
35               super();
36           }
37  
38           public ChainedException(String message) {
39               super(message);
40           }
41  
42           public ChainedException(String message, Throwable cause) {
43               super(message);
44               this.cause = cause;
45           }
46  
47           public ChainedException(Throwable cause) {
48               super();
49               this.cause = cause;
50           }
51  
52           public Throwable getCause() {
53               return this.cause;
54           }
55  
56           public boolean isCause() {
57               return (this.cause!=null);
58           }
59  
60           public String getCauseMessage() {
61               if (this.cause==null)
62                  return null;
63               return this.cause.getMessage();
64           }
65  
66           public void getMessage(StringBuffer sb) {
67               sb.append(super.getMessage());
68               sb.append(ConvertUtils.LINE_FEED);
69               if (cause != null) {
70                   sb.append(CAUSED_BY);
71                   if (cause instanceof ChainedException) {
72                       ChainedException chainedCause = (ChainedException) cause;
73                       chainedCause.getMessage(sb);
74                   }
75                   else {
76                       sb.append(cause.getMessage());
77                   }
78               }
79           }
80  
81           public void printStackTrace() {
82               super.printStackTrace();
83               if (cause != null) {
84                   System.err.println(CAUSED_BY);
85                   cause.printStackTrace();
86               }
87           }
88  
89           public void printStackTrace(java.io.PrintStream ps) {
90               super.printStackTrace(ps);
91               if (cause != null) {
92                   ps.println(CAUSED_BY);
93                   cause.printStackTrace(ps);
94               }
95           }
96  
97           public void printStackTrace(java.io.PrintWriter pw) {
98               super.printStackTrace(pw);
99               if (cause != null) {
100                  pw.println(CAUSED_BY);
101                  cause.printStackTrace(pw);
102              }
103          }
104     }
105