KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > BaseException


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util;
8
9
10 import java.util.Iterator JavaDoc;
11
12 import com.inversoft.error.ErrorList;
13
14
15 /**
16  * This is the base exception for all InverSoft exceptions. It
17  * adds the functionality of the ErrorList to the Exceptions
18  * so that all Exceptions can carry an ErrorList around with
19  * them.
20  *
21  * @author Brian Pontarelli
22  * @since 2.0
23  * @version 2.0
24  */

25 public class BaseException extends Exception JavaDoc {
26
27     private ErrorList errors = new ErrorList();
28
29     /**
30      * Constructs a new empty <code>BaseException</code> with an emptry
31      * but not null ErrorList.
32      */

33     public BaseException() {
34         super();
35     }
36
37     /**
38      * Constructs a new <code>BaseException</code> with the given error
39      * message and an empty but not null ErrorList. This error message is
40      * added to the ErrorList for this Exception as well as being set as
41      * the message retrieved from the getMessage() method.
42      *
43      * @param message The error message for the exception
44      */

45     public BaseException(String JavaDoc message) {
46         super(message);
47         errors.addError(message);
48     }
49
50     /**
51      * Constructs a new <code>BaseException</code> with the given error
52      * message, root cause and an empty but not null ErrorList. This error
53      * message is added to the ErrorList for this Exception as well as being
54      * set as the message retrieved from the getMessage() method.
55      *
56      * @param message The error message for the exception
57      * @param cause The root cause throwable
58      */

59     public BaseException(String JavaDoc message, Throwable JavaDoc cause) {
60         super(message, cause);
61
62         if (cause instanceof BaseException) {
63             errors = ((BaseException) cause).getErrors();
64         } else {
65             errors.addError(message);
66         }
67     }
68
69     /**
70      * Constructs a new <code>BaseException</code> with the given root cause
71      * and an empty but not null ErrorList
72      *
73      * @param cause The root cause throwable
74      */

75     public BaseException(Throwable JavaDoc cause) {
76         super(cause);
77
78         if (cause instanceof BaseException) {
79             errors = ((BaseException) cause).getErrors();
80         } else {
81             errors.addError(cause.getMessage());
82         }
83     }
84
85     /**
86      * Constructs a new <code>BaseException</code> with the given ErrorList and
87      * message
88      *
89      * @param message The error message for the exception
90      * @param errors The ErrorList
91      */

92     public BaseException(String JavaDoc message, ErrorList errors) {
93         super(message);
94         this.errors = errors;
95     }
96
97     /**
98      * Constructs a new <code>BaseException</code> with the given ErrorList
99      *
100      * @param errors The ErrorList
101      */

102     public BaseException(ErrorList errors) {
103         super();
104         this.errors = errors;
105     }
106
107
108     /**
109      * Gets the error list for this Exception
110      *
111      * @return Returns the error list for this Exception
112      */

113     public ErrorList getErrors() {
114         return errors;
115     }
116
117     /**
118      * Sets the error list for this Exception. This overrides the default
119      * ErrorList instance
120      *
121      * @param errors the error list for this Exception
122      */

123     public void setErrors(ErrorList errors) {
124         this.errors = errors;
125     }
126
127     /**
128      * This method overrides the default toString from Exception so that the
129      * message also contains all the errors in the error list, if there any.
130      */

131     public String JavaDoc toString() {
132         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
133
134         if (!StringTools.isEmpty(getMessage())) {
135             buf.append(getMessage());
136         }
137
138         buf.append("\nError list:\n");
139         Iterator JavaDoc iter = errors.iterator();
140         while(iter.hasNext()) {
141             buf.append(iter.next().toString());
142             buf.append("\n");
143         }
144
145         return buf.toString();
146     }
147 }
148
Popular Tags