Results 1 to 5 of 5
  1. #1
    MrDave is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default Dealing with IndexOutOfBoundsExceptions

    I'm just wondering what the best method is for dealing with IndexOutOfBoundsExceptions at the moment I use something like this:

    Java Code:
    public Person getPersonAtIndex(int index){
            try{
                return this.m_people.elementAt(index);
            }catch(IndexOutOfBoundsException e){
                return null;
            }
        }
    But returning null doesn't really seem like the best solution to me, is there a better way of dealing with this.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    Have the method throw the exception itself and pass it to where the code that gen'ed the bad index can handle it.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Just like Norm said, this way you don't have to worry about what the returned null means, since it could be a valid value. This works just fine:
    Java Code:
    public Person getPersonAtIndex(int index) throws IndexOutOfBoundsException {
            if(index >= m_people.size())
                throw new IndexOutOfBoundsException();      
            return this.m_people.elementAt(index);
        }
    Then you catch the exception in your driver class.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    Java Code:
    public Person getPersonAtIndex(int index) throws IndexOutOfBoundsException {
            return this.m_people.elementAt(index);
    }
    This would also work.
    The other gives you a place to put some debug code before the exception is thrown.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Just a quick note; if using m00nchile's method, you may also want to check if index < 0. The likelihood is that you will never get a call like getPersonAtIndex(-1) but it could happen.

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2009, 09:33 PM
  2. dealing with (zip) files
    By aloula in forum Advanced Java
    Replies: 4
    Last Post: 10-06-2009, 08:32 PM
  3. Replies: 2
    Last Post: 09-23-2009, 07:46 PM
  4. Dealing with iReport 3.0.0
    By HotEvilGirl in forum New To Java
    Replies: 6
    Last Post: 09-11-2009, 12:32 PM
  5. Inconsistencies dealing with null
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-11-2009, 09:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •