Results 1 to 3 of 3
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default StringIndexOutOfBoundsException

    Review the code below:

    Java Code:
    try {
    arrayList.get(10);
    } catch (StringIndexOutOfBoundsException e) {
    ...
    }
    I might get two exceptions here.
    - StringIndexOutOfBoundsException
    - IndexOutOfBoundsException

    StringIndexOutOfBoundsException is subclass of IndexOutOfBoundsException. So I believe that my catch will catch both Exceptions. Is this right?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    It should be IndexOutOfBoundsException. But as this exception is a runtime exception it is ok if you don't catch it. The better solution is to check the size of your array list before you try to read data at the speficied index.

  3. #3
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Quote Originally Posted by ravian View Post

    StringIndexOutOfBoundsException is subclass of IndexOutOfBoundsException. So I believe that my catch will catch both Exceptions. Is this right?
    I think it actually has to be IndexOutOfBoundsException that needs to be caught. Because, if you were to put Exception e instead, it catches any type of Exception. So, its the same case here, the superclass is the one that needs to be caught. Correct me if I'm wrong.

Posting Permissions

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