What would you throw?

Discussion in 'Java' started by Ian Pilcher, Feb 25, 2006.

  1. Ian Pilcher

    Ian Pilcher Guest

    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?

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Feb 25, 2006
    #1
    1. Advertising

  2. Ian Pilcher

    VisionSet Guest

    "Ian Pilcher" <> wrote in message
    news:...
    > 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

    --
    Mike W
     
    VisionSet, Feb 25, 2006
    #2
    1. Advertising

  3. There is nothing wrong with argument passed though, it is the data
    structure which cannot cope, so I say IllegalStateException

    Opalinski

    http://www.geocities.com/opalpaweb/
     
    opalinski from opalpaweb, Feb 25, 2006
    #3
  4. Ian Pilcher

    none Guest

    > 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/
     
    none, Feb 25, 2006
    #4
  5. Ian Pilcher

    VisionSet Guest

    <> wrote in message
    news:...
    > 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.
     
    VisionSet, Feb 25, 2006
    #5
  6. Ian Pilcher

    Alun Harford Guest

    "Ian Pilcher" <> wrote in message
    news:...
    > 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
     
    Alun Harford, Feb 25, 2006
    #6
  7. Ian Pilcher

    Ian Pilcher Guest

    Alun Harford wrote:
    > 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!

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Feb 25, 2006
    #7
  8. Ian Pilcher

    Chris Uppal Guest

    Ian Pilcher wrote:

    > 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
     
    Chris Uppal, Feb 25, 2006
    #8
  9. Ian Pilcher

    Roedy Green Guest

    On Sat, 25 Feb 2006 09:26:39 -0000, "Chris Uppal"
    <-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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 25, 2006
    #9
  10. Ian Pilcher

    Chris Uppal Guest

    Roedy Green wrote:

    > 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
     
    Chris Uppal, Feb 25, 2006
    #10
  11. Ian Pilcher

    Alun Harford Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:4400274e$0$1168$...
    > Ian Pilcher wrote:
    >
    > > 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.


    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
     
    Alun Harford, Feb 25, 2006
    #11
  12. Ian Pilcher

    Ian Pilcher Guest

    Alun Harford wrote:
    > 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

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Feb 26, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    13,033
    Kevin Spencer
    Oct 27, 2003
  2. Replies:
    15
    Views:
    7,557
    Roedy Green
    Sep 8, 2005
  3. Replies:
    0
    Views:
    1,130
  4. Emanuele D'Arrigo

    To throw or to throw not?

    Emanuele D'Arrigo, Nov 14, 2008, in forum: Python
    Replies:
    6
    Views:
    321
    Emanuele D'Arrigo
    Nov 15, 2008
  5. Alexander
    Replies:
    620
    Views:
    8,249
    Seebs
    Nov 9, 2010
Loading...

Share This Page