What would you throw?

I

Ian Pilcher

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?
 
V

VisionSet

Ian Pilcher said:
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?

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
 
N

none

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.
 
V

VisionSet

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.
 
A

Alun Harford

Ian Pilcher said:
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?

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
 
I

Ian Pilcher

Alun said:
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!
 
C

Chris Uppal

Ian said:
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
 
R

Roedy Green

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.
 
C

Chris Uppal

Roedy said:
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
 
A

Alun Harford

Chris Uppal said:
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.

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
 
I

Ian Pilcher

Alun said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top