Q:

Is VS Debugger Wrong? IndexOutOfRangeException thrown by the code: f++

Why is an exception being thrown in the "f++" part of the code below ("IndexOutOfRangeException was unhandled by user code"):
for (int f = 0; f < gnf; f++)  
{  
    fieldNames[g] = grid.FieldName(f);  
}  

The bug is in the "fieldNames[g] = ..." part of the code, my algorithm should be:
for (int f = 0; f < gnf; f++)  
{  
    fieldNames[f] = grid.FieldName(f);  
}  

(This does not crash.) But the debugger is not showing the exception on the "fieldNames[g]..." line when the wrong (top) code runs.
I'm not using threads at this point so I don't see it being one of those "debugging exceptions in a threaded program is suicide" situations.
Why is the debugger showing the exception in the wrong place? Optimizations or something? Has anyone else had the debugger be "wrong" like this before?

A:

Could be the optimizer. Is your build set to DEBUG or RELEASE?

