KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > java > util > ExtNoSuchElementException


1 /*
2  * ExtNoSuchElementException.java: Extended standard throwable for stacks
3  *
4  * Copyright (C) 2001 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.util;
32
33 //------------------------------------------------------------------------------
34
// Imports
35
//
36
import java.util.NoSuchElementException JavaDoc;
37
38 import de.susebox.java.lang.ThrowableList;
39 import de.susebox.java.lang.ThrowableMessageFormatter;
40
41
42 //------------------------------------------------------------------------------
43
// ExtNoSuchElementException - definition
44
//
45

46 /**
47  * Implementation of the {@link ThrowableList} interface for the JDK exception
48  * {@link java.util.NoSuchElementException}.
49  *
50  * @author Heiko Blau
51  */

52 public class ExtNoSuchElementException
53   extends NoSuchElementException JavaDoc
54   implements ThrowableList
55 {
56   //---------------------------------------------------------------------------
57
// methods of the ThrowableList interface
58
//
59

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

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

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

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

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

111   public Object JavaDoc[] getArguments() {
112     return _args;
113   }
114   
115   
116   //---------------------------------------------------------------------------
117
// constructors
118
//
119

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

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

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

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

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

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

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

224   public String JavaDoc getMessage() {
225     return ThrowableMessageFormatter.getMessage(this);
226   }
227   
228   //---------------------------------------------------------------------------
229
// members
230
//
231

232   /**
233    * the parameters to be used when formatting the throwable message
234    */

235   protected Object JavaDoc[] _args = null;
236
237   /**
238    * The wrapped, nested of next throwable.
239    */

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

247   protected boolean _isWrapper = false;
248 }
249
Popular Tags