Results 1 to 9 of 9

Thread: indexoutofrange exception

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    indexoutofrange exception

    OK, I'm a newbie, but please explain why the following doesn't give an indexoutofrange exception:

    Dim ix(2) As Integer
    For i As Integer = 0 To 3
    ix(i) = i
    Next

    If I put it in a try..catch block, it catches the error, but if I don't then the program continues merrily on its way. Seems to me that in debugging mode, there should be some indication that an out of range exception has occurred without having to explicitly check for it. Admittedly, in this instance it's obvious, but in a more complex program, it might not be.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: indexoutofrange exception

    It's important when using arrays to understand the relationship between the length and the upper bound and when to use each one. The length is the number of elements in the array and the upper bound is the index of the last element. Basically all .NET arrays are zero-based, i.e. the index of the first element is zero, so the upper bound is one less than the length.

    When you create an array the way you are, you specify the upper bound. You are specifying 2 as the upper bound so the last element has an index of 2. Now look at your For loop. Can you see what's wrong with it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: indexoutofrange exception

    OK, I just read your post more carefully and I was obviously answering the wrong question. To answer the question you asked, it DOES throw an exception. Almost certainly you have that code in the Load event handler of your startup form. On x64 systems, exceptions thrown in the Load event handler of the startup form (not sure about other forms) are swallowed. I'm not sure what the reason for that is but it's well-known and well-documented behaviour so you could fairly easily find out.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Re: indexoutofrange exception

    Thanks for the quick reply. Yes, I know the code is wrong. Now I know why it isn't caught. And yes indeed if I copy the code to an event handler, for example, it does indeed raise an exception.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Re: indexoutofrange exception

    Oh, and by the way, if it is, as you say, well-documented, where is it so documented? It certainly isn't in the VB help file.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: indexoutofrange exception

    It's an internal pissing match between the OS group and the development tools group. I wouldn't say that it is explicitly documented, but it is something that has caused lengthy discussions on forums like these. I'd say that it is a widely recognized gotcha rather than being well documented, since the latter sounds official, and this seem to be more of an embarrasment.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: indexoutofrange exception

    When I said "well-documented" I actually meant that a lot has been written about it, not necessarily that there was an MSDN topic on the subject.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    43

    Re: indexoutofrange exception

    Quote Originally Posted by Shaggy Hiker View Post
    It's an internal pissing match between the OS group and the development tools group. I wouldn't say that it is explicitly documented, but it is something that has caused lengthy discussions on forums like these. I'd say that it is a widely recognized gotcha rather than being well documented, since the latter sounds official, and this seem to be more of an embarrasment.
    Sounds like there should be a FAQ entitled "Widely Recognized Gotchas" for newbies like me.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: indexoutofrange exception

    There probably should be. The problem is that if they become sufficiently widely known, they cease to become gotchas. Therefore, if we publicized such a list, it would immediately self-destruct, and might take out our computers.
    My usual boring signature: Nothing

Posting Permissions

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



Click Here to Expand Forum to Full Width