KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > exception > NestedException


1 /*
2  * Copyright 2004 Clinton Begin
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 package com.ibatis.common.exception;
17
18 /**
19  * Nexted exception implementation. Thanks Claus.
20  */

21
22 public class NestedException extends Exception JavaDoc {
23
24   private static final String JavaDoc CAUSED_BY = "\nCaused by: ";
25
26   private Throwable JavaDoc cause = null;
27
28   /**
29    * Constructor
30    */

31   public NestedException() {
32   }
33
34   /**
35    * Constructor
36    *
37    * @param msg error message
38    */

39   public NestedException(String JavaDoc msg) {
40     super(msg);
41   }
42
43   /**
44    * Constructor
45    *
46    * @param cause the nested exception (caused by)
47    */

48   public NestedException(Throwable JavaDoc cause) {
49     super();
50     this.cause = cause;
51   }
52
53   /**
54    * Constructor
55    *
56    * @param msg error message
57    * @param cause the nested exception (caused by)
58    */

59   public NestedException(String JavaDoc msg, Throwable JavaDoc cause) {
60     super(msg);
61     this.cause = cause;
62   }
63
64   /**
65    * Gets the causing exception, if any.
66    *
67    * @return The cause of the exception
68    */

69   public Throwable JavaDoc getCause() {
70     return cause;
71   }
72
73   /**
74    * Converts the exception to a string representation
75    *
76    * @return The string representation of the exception
77    */

78   public String JavaDoc toString() {
79     if (cause == null) {
80       return super.toString();
81     } else {
82       return super.toString() + CAUSED_BY + cause.toString();
83     }
84   }
85
86   /**
87    * Sends a stack trace to System.err (including the root cause, if any)
88    */

89   public void printStackTrace() {
90     super.printStackTrace();
91     if (cause != null) {
92       System.err.println(CAUSED_BY);
93       cause.printStackTrace();
94     }
95   }
96
97   /**
98    * Sends a stack trace to the PrintStream passed in (including the root cause, if any)
99    *
100    * @param ps - the PrintStream to send the output to
101    */

102   public void printStackTrace(java.io.PrintStream JavaDoc ps) {
103     super.printStackTrace(ps);
104     if (cause != null) {
105       ps.println(CAUSED_BY);
106       cause.printStackTrace(ps);
107     }
108   }
109
110   /**
111    * Sends a stack trace to the PrintWriter passed in (including the root cause, if any)
112    *
113    * @param pw - the PrintWriter to send the output to
114    */

115   public void printStackTrace(java.io.PrintWriter JavaDoc pw) {
116     super.printStackTrace(pw);
117     if (cause != null) {
118       pw.println(CAUSED_BY);
119       cause.printStackTrace(pw);
120     }
121   }
122
123 }
124
Popular Tags