1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.util;
22
23 import java.io.PrintStream;
24 import java.io.PrintWriter;
25
26 /**
27 * A checked chained exception.
28 *
29 * @version $Id: ChainedException.java 238991 2004-05-22 11:34:50Z vmassol $
30 */
31 public class ChainedException extends Exception
32 {
33 /**
34 * Original exception which caused this exception.
35 */
36 protected Throwable originalException;
37
38 /**
39 * Create a <code>ChainedException</code> and set the exception error
40 * message.
41 *
42 * @param theMessage the message of the exception
43 */
44 public ChainedException(String theMessage)
45 {
46 this(theMessage, null);
47 }
48
49 /**
50 * Create a <code>ChainedException</code>, set the exception error
51 * message along with the exception object that caused this exception.
52 *
53 * @param theMessage the detail of the error message
54 * @param theException the original exception
55 */
56 public ChainedException(String theMessage, Throwable theException)
57 {
58 super(theMessage);
59 this.originalException = theException;
60 }
61
62 /**
63 * Create a <code>ChaineException</code>, and set exception object
64 * that caused this exception. The message is set by default to be the one
65 * from the original exception.
66 *
67 * @param theException the original exception
68 */
69 public ChainedException(Throwable theException)
70 {
71 super(theException.getMessage());
72 this.originalException = theException;
73 }
74
75 /**
76 * Print the full stack trace, including the original exception.
77 */
78 public void printStackTrace()
79 {
80 printStackTrace(System.err);
81 }
82
83 /**
84 * Print the full stack trace, including the original exception.
85 *
86 * @param thePs the byte stream in which to print the stack trace
87 */
88 public void printStackTrace(PrintStream thePs)
89 {
90 super.printStackTrace(thePs);
91
92 if (this.originalException != null)
93 {
94 this.originalException.printStackTrace(thePs);
95 }
96 }
97
98 /**
99 * Print the full stack trace, including the original exception.
100 *
101 * @param thePw the character stream in which to print the stack trace
102 */
103 public void printStackTrace(PrintWriter thePw)
104 {
105 super.printStackTrace(thePw);
106
107 if (this.originalException != null)
108 {
109 this.originalException.printStackTrace(thePw);
110 }
111 }
112 }