Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

What would you throw?

Thread view: 
Ian Pilcher - 25 Feb 2006 00:23 GMT
One of these "not gonna happen before the heat death of the Universe"
questions that drive me nuts.

Map.size() returns an int, so what should I throw if someone tries to
add more than Integer.MAX_VALUE entries to my map -- which could happen
on a 64-bit JVM?

   IllegalStateException?
   ArithmeticException?

Thoughts?

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

VisionSet - 25 Feb 2006 00:38 GMT
> One of these "not gonna happen before the heat death of the Universe"
> questions that drive me nuts.
[quoted text clipped - 7 lines]
>
> Thoughts?

IllegalArgumentException?

Consistency is the key.  How do you test your app?
Do you want to look up every time to see what you throw?
For this reason I like IllegalArgumentException

--
Mike W
opalpa@gmail.com opalinski from opalpaweb - 25 Feb 2006 00:41 GMT
There is nothing wrong with argument passed though, it is the data
structure which cannot cope, so I say IllegalStateException

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
VisionSet - 25 Feb 2006 00:48 GMT
<opalpa@gmail.com> wrote in message
news:1140828103.563105.266130@u72g2000cwu.googlegroups.com...
> There is nothing wrong with argument passed though, it is the data
> structure which cannot cope, so I say IllegalStateException

Yes, of couirse, sorry reasonogjing p[oerwrs reduiced since quite drujnik.
none - 25 Feb 2006 00:42 GMT
> add more than Integer.MAX_VALUE entries to my map -- which could happen
> on a 64-bit JVM?

Who told you that? The tooth fairy? It is untrue, which moots the
remainder of your question.

--
Tony Morris
http://tmorris.net/
Alun Harford - 25 Feb 2006 01:59 GMT
> One of these "not gonna happen before the heat death of the Universe"
> questions that drive me nuts.
[quoted text clipped - 7 lines]
>
> Thoughts?

This is defined in the API spec for java.util.Map.size()
"Returns the number of key-value mappings in this map. If the map contains
more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE."

Alun Harford
Ian Pilcher - 25 Feb 2006 03:55 GMT
> This is defined in the API spec for java.util.Map.size()
> "Returns the number of key-value mappings in this map. If the map contains
> more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE."

D'oh!

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================

Chris Uppal - 25 Feb 2006 09:26 GMT
> Map.size() returns an int, so what should I throw if someone tries to
> add more than Integer.MAX_VALUE entries to my map

Why should you care ?

Leaving aside the observation that it's unrealisitic to worry about such
things, it's the /map's/ job to determine its capacity not yours.  If it can't
accept another entry then it should throw something, it's not your
responsibility to police /its/ limitations.

OTOH, if you are creating your own implementation of Map, then either you
really care about this situation, in which case I don't see why you should code
your Map to have this limitation, or you don't and are just painting the lily,
in which case do whatever seems convenient.  Throw a custom exception, throw an
ill-choosen exception (OutOfMemory ?), go into an infinite loop, whatever takes
your fancy.

   -- chris
Roedy Green - 25 Feb 2006 10:24 GMT
On Sat, 25 Feb 2006 09:26:39 -0000, "Chris Uppal"
<chris.uppal@metagnostic.REMOVE-THIS.org> wrote, quoted or indirectly
quoted someone who said :

>Throw a custom exception, throw an
>ill-choosen exception (OutOfMemory ?), go into an infinite loop, whatever takes
>your fancy.

The one I end up using is IllegalArgumentException but that most of
the time is not a very accurate name for what has happened.

There should be standard  one with a name like BadData or
MalformedData to complate about the format of data in files.

Maybe  ProblemTooBigException.. It is easy to get carried away.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Uppal - 25 Feb 2006 13:50 GMT
> There should be standard  one with a name like BadData or
> MalformedData to complate about the format of data in files.

FWIW, IllegalStateException seems to fit Ian's requirements exactly.
You have asked an object to perform an operation when that object is
not in a state where it can comply.

But, I stick by my earlier answer.

   -- chris
Alun Harford - 25 Feb 2006 17:02 GMT
> > Map.size() returns an int, so what should I throw if someone tries to
> > add more than Integer.MAX_VALUE entries to my map
[quoted text clipped - 5 lines]
> accept another entry then it should throw something, it's not your
> responsibility to police /its/ limitations.

If you might put more than 2^31 entries into a map, you need to deal with
the fact that the interface is badly defined so that size() return an int.

> OTOH, if you are creating your own implementation of Map, then either you
> really care about this situation, in which case I don't see why you should code
> your Map to have this limitation,

You don't have a choice. Map.size() returns an int. Clearly, because the map
can contain more than Integer.MAX_SIZE elements, it _should_ return a long -
and Sun have had to do a nasty hack to keep backwards compatibility.

Alun Harford
Ian Pilcher - 26 Feb 2006 02:47 GMT
> You don't have a choice. Map.size() returns an int. Clearly, because the map
> can contain more than Integer.MAX_SIZE elements, it _should_ return a long -
> and Sun have had to do a nasty hack to keep backwards compatibility.

Sun's nasty hack breaks every bit of code out there that relies on size
returning a meaningful value, including their own AbstractSet.  Demo at:

   http://home.comcast.net/~i.pilcher/IntegerSet.java

Signature

========================================================================
Ian Pilcher                                        i.pilcher@comcast.net
========================================================================



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.