KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > java > lang > ExtNullPointerException


1 /*
2  * ExtNullPointerException.java: Extended standard throwable for stacks
3  *
4  * Copyright (C) 2002 Heiko Blau
5  *
6  * This file belongs to the Susebox Java Core Library (Susebox JCL).
7  * The Susebox JCL is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or (at your
10  * option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with the Susebox JCL. If not, write to the
19  *
20  * Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330,
22  * Boston, MA 02111-1307
23  * USA
24  *
25  * or check the Internet: http://www.fsf.org
26  *
27  * Contact:
28  * email: heiko@susebox.de
29  */

30
31 package de.susebox.java.lang;
32
33 //------------------------------------------------------------------------------
34
// Imports
35
//
36
import java.lang.NullPointerException JavaDoc;
37
38
39 //------------------------------------------------------------------------------
40
// ExtNullPointerException - definition
41
//
42

43 /**
44  * Implementation of the {@link ThrowableList} interface for the well-knwon JDK
45  * {@link java.lang.NullPointerException}.
46  *
47  * @version 1.00, 2001/06/26
48  * @author Heiko Blau
49  */

50 public class ExtNullPointerException extends NullPointerException JavaDoc implements ThrowableList {
51   
52   //---------------------------------------------------------------------------
53
// methods of the ThrowableList interface
54
//
55

56   /**
57    * Retrieving the cause of a <code>Throwable</code>. This is the method introduced
58    * with JDK 1.4. It replaces the older {@link #nextThrowable}.
59    *
60    * @return the cause of this <code>Throwable</code>
61    * @see java.lang.Throwable#getCause
62    */

63   public Throwable JavaDoc getCause() {
64     return _next;
65   }
66   
67   /**
68    * Method to traverse the list of {@link java.lang.Throwable}.
69    * See {@link ThrowableList#nextThrowable} for details.
70    *
71    * @return the "earlier" throwable
72    * @deprecated use the JDK 1.4 call {@link #getCause} instead
73    */

74   public Throwable JavaDoc nextThrowable() {
75     return getCause();
76   }
77
78   /**
79    * Check if <code>this</code> is only a throwable that wraps the real one. See
80    * {@link ThrowableList#isWrapper} for details.
81    *
82    * @return <code>true</code> if this is a wrapper exception,
83    * <code>false</code> otherwise
84    */

85   public boolean isWrapper() {
86     return _isWrapper;
87   }
88   
89   /**
90    * Getting the format string of a exception message. This can also be the
91    * message itself if there are no arguments.
92    *
93    * @return the format string being used by {@link java.text.MessageFormat}
94    * @see #getArguments
95    */

96   public String JavaDoc getFormat() {
97     return super.getMessage();
98   }
99   
100   /**
101    * Retrieving the arguments for message formats. These arguments are used by
102    * the {@link java.text.MessageFormat#format} call.
103    *
104    * @return the arguments for a message format
105    * @see #getFormat
106    */

107   public Object JavaDoc[] getArguments() {
108     return _args;
109   }
110   
111   
112   //---------------------------------------------------------------------------
113
// constructors
114
//
115

116   /**
117    * This constructor takes a simple message string like ordinary Java
118    * {@link java.lang.Throwable} classes. This is the most convenient form to
119    * construct an <code>ThrowableList</code> throwable.
120    *
121    * @param msg message for this <code>Throwable</code> instance
122    */

123   public ExtNullPointerException(String JavaDoc msg) {
124     this(null, msg, null);
125   }
126   
127   /**
128    * This constructor should be used for wrapping another exception. While reading
129    * data an IOException may occur, but a certain interface requires a
130    * <code>SQLException</code>. Simply use:
131    *<blockquote><pre>
132    * try {
133    * ...
134    * } catch (NullPointerException ex) {
135    * throw new ExtRuntimeException(ex);
136    * }
137    *</pre></blockquote>
138    *
139    * @param ex the exception to wrap
140    */

141   public ExtNullPointerException(Throwable JavaDoc ex) {
142     this(ex, null, null);
143   }
144   
145   /**
146    * If one likes to add ones own information to an exception, this constructor is
147    * the easiest way to do so. By using such an approach a exception trace with useful
148    * additional informations (which file could be found, what username is unknown)
149    * can be realized:
150    *<blockquote><pre>
151    * try {
152    * ...
153    * } catch (SQLException ex) {
154    * throw new IOException(ex, "while connecting to " + url);
155    * }
156    *</pre></blockquote>
157    *
158    * @param ex the inner throwable
159    * @param msg throwable message
160    */

161   public ExtNullPointerException(Throwable JavaDoc ex, String JavaDoc msg) {
162     this(ex, msg, null);
163   }
164   
165   /**
166    * This constructor takes a format string and its arguments. The format string
167    * must have a form that can be used by {@link java.text.MessageFormat} methods.
168    * That means:
169    *<blockquote><pre>
170    * java.text.Message.format(fmt, args)
171    *</pre></blockquote>
172    * is similar to
173    *<blockquote><pre>
174    * new MyException(fmt, args).getMessage();
175    *</pre></blockquote>
176    *
177    * @param fmt throwable message
178    * @param args arguments for the given format string
179    */

180   public ExtNullPointerException(String JavaDoc fmt, Object JavaDoc[] args) {
181     this(null, fmt, args);
182   }
183   
184   /**
185    * This is the most complex way to construct an <code>ExceptionList</code>-
186    * Throwable.<br>
187    * An inner throwable is accompanied by a format string and its arguments.
188    * Use this constructor in language-sensitive contexts or for formalized messages.
189    * The meaning of the parameters is explained in the other constructors.
190    *
191    * @param ex the inner throwable
192    * @param fmt throwable message
193    * @param args arguments for the given format string
194    */

195   public ExtNullPointerException(Throwable JavaDoc ex, String JavaDoc fmt, Object JavaDoc[] args) {
196     super(fmt);
197    
198     if (ex != null && fmt == null) {
199       _isWrapper = true;
200     } else {
201       _isWrapper = false;
202     }
203     _next = ex;
204     _args = args;
205   }
206   
207   
208   //---------------------------------------------------------------------------
209
// overridden methods
210
//
211

212   /**
213    * Implementation of the standard {@link java.lang.Throwable#getMessage} method. It
214    * delegates the call to the central {@link ThrowableMessageFormatter#getMessage}
215    * method.
216    *
217    * @return the formatted throwable message
218    * @see ThrowableMessageFormatter
219    */

220   public String JavaDoc getMessage() {
221     return ThrowableMessageFormatter.getMessage(this);
222   }
223   
224   //---------------------------------------------------------------------------
225
// members
226
//
227

228   /**
229    * the parameters to be used when formatting the throwable message
230    */

231   protected Object JavaDoc[] _args = null;
232
233   /**
234    * The wrapped, nested of next throwable.
235    */

236   protected Throwable JavaDoc _next = null;
237   
238   /**
239    * If <code>true</code> this is only a wrapper throwable with the real one
240    * being returned by {@link #nextThrowable}, <code>false</code> for standalone,
241    * nested or subsequent exceptions
242    */

243   protected boolean _isWrapper = false;
244 }
245
Popular Tags