|
-
Jun 25th, 2012, 08:42 PM
#1
Thread Starter
Member
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.
-
Jun 25th, 2012, 08:53 PM
#2
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?
-
Jun 25th, 2012, 08:56 PM
#3
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.
-
Jun 25th, 2012, 09:24 PM
#4
Thread Starter
Member
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.
-
Jun 25th, 2012, 09:26 PM
#5
Thread Starter
Member
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.
-
Jun 25th, 2012, 09:37 PM
#6
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
 
-
Jun 25th, 2012, 09:41 PM
#7
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.
-
Jun 26th, 2012, 09:53 AM
#8
Thread Starter
Member
Re: indexoutofrange exception
 Originally Posted by Shaggy Hiker
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.
-
Jun 26th, 2012, 10:24 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|