views:

98

answers:

2

The usual behaviour of VS is to display in yellow the faulting line :

    int i = 1;
    switch (i)
    {
        default:
            throw new NotImplementedException(); //this will be yellow
    }

However, quite frequently, I've witnessed that the wrong line is coloured, like in this example :

        int i = 1;
        switch (i)
        {
            default:
                if (i==1)
                     throw new NotImplementedException();
                break;  i = 1; //this line is displayed in yellow, which makes no sense
        }

While I understand some of the differences between those two snippets (in the first situation, the "throw..." line is the last executable line of the method, so no IL is generated for the code coming after this, thus it's the last possible line to "display" the exception on), I'm wondering while VS fails to display the exception on the correct line in the second snippet.

Has someone any hints on the reason of this strange behaviour ? Despite knowing about this caveat, once in a while I end up spending a few minutes debugging the wrong line.

+1  A: 

If the code has been updated, but the code has not been compiled (happens a lot with edit and continue), the line numbers will be off. I've had this happen quite a bit before. This is one reason I am sure of where this happens.

The other case I know of where this happens is when you have DLL dependencies that are not rebuilt for one reason or another (Visual studio sometimes gets its DLL dependency tree fubared), it will highlight the wrong lines while debugging as well. This is similar to the edit and continue problem.

The other thing I can think of is that very rarely I have noticed that Studio will highlight the line that the code will return to... after throwing an exception, and hitting continue, the code will continue execution on x line.. But I don't exactly know how to reproduce it.

Lomilar
+1  A: 

I think the key is the "if" clause. I have seen the same behavior just stepping through code. If there are not "{}" around the single statement in the if block, the current statement can be indicated as the line after the line you expect.

Larry Fix
In older versions of studio, I believe you are correct. I haven't seen this since 2K5. Maybe its still around.
Lomilar