[jdom-interest] [Vote] NoSuchChildException
Alex Chaffee
guru at edamame.stinky.com
Thu Jul 13 03:00:37 PDT 2000
On Thu, Jul 13, 2000 at 08:05:22AM -0400, Elliotte Rusty Harold wrote:
> At 12:13 AM -0700 7/13/00, Alex Chaffee wrote:
>
> >Element is a collection. If a collection has no such element it
> >returns null. QED.
> >
>
> Your second premise is flawed, therefore the syllogism fails. The
> Java Collections API is extremely schizophrenic on what to do when no
> such element exists. Some methods return null. Some methods throw a
> NoSuchElementException. Some methods throw an
> IndexOutOfBoundsException.
Not true.
NoSuchElementException is only thrown by one class, Enumeration, which
belongs to the JDK 1.0 flawed collections API. The JDK 1.2
collections (which, IMHO, should have been named java.util.collections
or javax.collections to avoid namespace and brainspace collisions with
Vector and friends) are very consistent in returning null from get()
methods.
Likewise, IndexOutOfBoundsException is only thrown from methods that
take an index, to wit, List and Vector. It's a correct use of an
exception, indeed of a RuntimeException, since such a case should
never ever ever happen in shipped code. Array indexes should always
be pre-bounds-checked; here Java is using exceptions for language
safety more than anything else.
> In fact we originally went to throwing an exception by analogy with
> the Collections API, and I'm certain we changed
> NoSuchElementException to NoSuchChildException precisely to avoid the
> name conflict with java.util.NoSuchElementException.
That's certainly possible, but irrelevant here :-)
Cheers -
- Alex
--
Alex Chaffee mailto:alex at jguru.com
jGuru - Java News and FAQs http://www.jguru.com/alex/
Creator of Gamelan http://www.gamelan.com/
Founder of Purple Technology http://www.purpletech.com/
Curator of Stinky Art Collective http://www.stinky.com/
More information about the jdom-interest
mailing list