KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > exception > JGossipException


1 /*
2  * $Id: JGossipException.java,v 1.3 2005/06/07 12:32:23 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitriy Belov <bel@jresearch.org>
23  * .
24  * * ***** END LICENSE BLOCK ***** */

25
26 package org.jresearch.gossip.exception;
27
28 import java.io.PrintStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.StringWriter JavaDoc;
31
32 /**
33  * Class <code>JGossipException</code> is a base class for all exceptions in
34  * application/
35  *
36  * @author <code>$Author: bel70 $</code>
37  * @version <code>$Revision: 1.3 $</code>
38  */

39 public class JGossipException extends Exception JavaDoc {
40
41     /**
42      *
43      */

44     private static final long serialVersionUID = -2799181321033636504L;
45
46     /** Field <code>nestedException</code> */
47     private Exception JavaDoc nestedException;
48
49     /**
50      * Constructor <code>JGossipException</code> creates empty exception
51      */

52     protected JGossipException() {
53         super();
54         nestedException = null;
55     }
56
57     /**
58      * Constructor <code>JGossipException</code> creates exception with error
59      * message
60      *
61      * @param <code>msg</code> error message
62      */

63     protected JGossipException(String JavaDoc msg) {
64         super(msg);
65         nestedException = null;
66     }
67
68     /**
69      * Constructor <code>JGossipException</code> create exception with
70      * originaly thrown one
71      *
72      * @param <code>cause</code> original exception
73      */

74     protected JGossipException(Exception JavaDoc cause) {
75         super();
76         nestedException = cause;
77     }
78
79     /**
80      * Constructor <code>JGossipException</code> create exception with error
81      * message and original exception
82      *
83      * @param <code>msg</code> error message
84      * @param <code>cause</code> original exception
85      */

86     protected JGossipException(String JavaDoc msg, Exception JavaDoc cause) {
87         super(msg);
88         nestedException = cause;
89     }
90
91     /**
92      * Method <code>getException</code> return original exception
93      *
94      * @return original exception
95      */

96     public Exception JavaDoc getNestedException() {
97         return nestedException;
98     }
99
100     /**
101      * Method <code>getMessage</code> return erorr message
102      *
103      * @return error message
104      */

105     public String JavaDoc getMessage() {
106
107         String JavaDoc msg = super.getMessage();
108         Exception JavaDoc ex = getNestedException();
109
110         if (ex != null) {
111             String JavaDoc s = ex.getMessage();
112
113             if (s != null) {
114                 if (msg == null)
115                     msg = "";
116                 else
117                     msg += "\n";
118                 msg += "CAUSE: " + s;
119             }
120         }
121         return msg;
122     }
123
124     /**
125      * Method <code>getStackTrace</code> return exception stack trace
126      *
127      * @return stack trace as String
128      */

129     public String JavaDoc getTrace() {
130
131         StringWriter JavaDoc strwrt = new StringWriter JavaDoc();
132
133         super.printStackTrace(new PrintWriter JavaDoc(strwrt));
134         return strwrt.toString();
135     }
136
137     /**
138      * Method <code>printStackTrace</code> print stack trace in given stream
139      *
140      * @param <code>out</code> stream to write trace to
141      */

142     public void printStackTrace(PrintStream JavaDoc out) {
143
144         Exception JavaDoc ex = getNestedException();
145
146         if (ex != null) {
147             ex.printStackTrace(out);
148             out.println("WRAPPING EXCEPTION:");
149         }
150         super.printStackTrace(out);
151     }
152
153     /**
154      * Method <code>printStackTrace</code> print stack trace in given stream
155      *
156      * @param <code>out</code> stream to write trace to
157      */

158     public void printStackTrace(PrintWriter JavaDoc out) {
159
160         Exception JavaDoc ex = getNestedException();
161
162         if (ex != null) {
163             ex.printStackTrace(out);
164             out.println("WRAPPING EXCEPTION:");
165         }
166         super.printStackTrace(out);
167     }
168
169     /**
170      * Method <code>printStackTrace</code> print stack trace to output
171      */

172     public void printStackTrace() {
173         printStackTrace(System.err);
174     }
175 }
176
Popular Tags