View Javadoc

1   /***
2    * @(#)NestedRuntimeException.java
3    * 
4    * JFoxSOAF, Service-Oriented Application Framework
5    * 
6    * Copyright(c) JFoxSOAF Team
7    * 
8    * Licensed under the GNU LGPL, Version 2.1 (the "License"); 
9    * you may not use this file except in compliance with the License. 
10   * You may obtain a copy of the License at  
11   * 
12   * http://www.gnu.org/copyleft/lesser.html
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, 
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17   * See the License for the specific language governing permissions and 
18   * limitations under the License. 
19   * 
20   * For more information, please visit:
21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22   * http://www.huihoo.org/jfox/jfoxsoaf
23   */
24  
25  package org.huihoo.jfox.soaf.exception;
26  
27  import java.io.PrintStream;
28  import java.io.PrintWriter;
29  
30  /***
31   * Superclass for all unchecked Exceptions in JFoxSOAF.
32   * 
33   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
34   * @version $Revision: 1.8 $ $Date: 2005/05/22 06:47:24 $
35   * @version Revision: 1.0
36   */
37  
38  public class NestedRuntimeException extends RuntimeException {
39  
40      /***
41       * The exception that caused this one.
42       */
43      private Throwable cause;
44  
45      /***
46       * Default Constructor.
47       */
48      public NestedRuntimeException() {
49          super();
50      }
51  
52      /***
53       * Construct a new exception with no cause and the specified detail message.
54       * Note modern JVMs may still track the exception that caused this one.
55       * 
56       * @param message the message detailing the exception.
57       */
58      public NestedRuntimeException(final String message) {
59          super(message);
60      }
61  
62      /***
63       * Construct a new exception with the specified cause and no detail message.
64       * 
65       * @param cause the exception that caused this one.
66       */
67      public NestedRuntimeException(final Throwable cause) {
68          this.cause = cause;
69      }
70  
71      /***
72       * Construct a new exception with the specified cause and the specified
73       * detail message.
74       * 
75       * @param message the message detailing the exception.
76       * @param cause the exception that caused this one.
77       */
78      public NestedRuntimeException(final String message, final Throwable cause) {
79          super(message);
80          this.cause = cause;
81      }
82  
83      /***
84       * Retrieve the exception that caused this one.
85       * 
86       * @return the exception that caused this one, or null if it was not set.
87       * @see Throwable#getCause() the method available since JDK 1.3 that is
88       *      overridden by this method.
89       */
90      public Throwable getCause() {
91          return cause;
92      }
93  
94      /***
95       * Returns the detail message string of this throwable.
96       * 
97       * @see java.lang.Throwable#getMessage()
98       */
99      public String getMessage() {
100         if (this.cause == null || this.cause == this) {
101             return super.getMessage();
102         } else {
103             return new StringBuffer().append(super.getMessage()).append(
104                     ": nested throwable: ").append(
105                     this.cause.getClass().getName()).append(": ").append(
106                     this.cause.getMessage()).toString();
107         }
108     }
109 
110     /***
111      * Prints this throwable and its backtrace to the standard error stream
112      * 
113      * @see java.lang.Throwable#printStackTrace()
114      */
115     public void printStackTrace() {
116         printStackTrace(System.err);
117     }
118 
119     /***
120      * Print the composite message and the embedded stack trace to the specified
121      * stream.
122      * 
123      * @param printStrem
124      */
125     public void printStackTrace(PrintStream printStrem) {
126         if (this.cause == null || this.cause == this) {
127             super.printStackTrace(printStrem);
128         } else {
129             cause.printStackTrace(printStrem);
130         }
131 
132     }
133 
134     /***
135      * Print the composite message and the embedded stack trace to the specified
136      * writer.
137      * 
138      * @param printWriter
139      */
140     public void printStackTrace(PrintWriter printWriter) {
141         if (this.cause == null || this.cause == this) {
142             super.printStackTrace(printWriter);
143         } else {
144             cause.printStackTrace(printWriter);
145         }
146 
147     }
148 
149 }