4

I run this sample application:

class Program
{
    static void Main()
    {
        var reader = System.Xml.XmlReader.Create(@"C:\nonXml.txt");
        while (reader.Read()) { }
        System.Console.WriteLine("Ok");
        System.Console.ReadKey();
    }
}

nonXml.txt is a one-line text file with non-xml content.

When I run the application without the debugger, reader.Read throws an expected XmlException and the application exits with error. When I run it with the debugger (F5 in Visual Studio), debugger signals the exception but after pressing F5 (Continue) the application unexpectedly continues normally and writes "Ok".

What's going on in the debug mode in this case?

foka
  • 804
  • 9
  • 27
  • 1
    you are using an XMLReader to read non-xml content.. why not use System.IO.StreamReader..? – MethodMan Oct 02 '12 at 15:30
  • @dj kraze: this is just an example. I'm asking about different behavior of the application with and without the debugger. I don't have problems with reading the files in general :-) – foka Oct 02 '12 at 15:48
  • Is Visual Studio set to break on all exceptions? Is it just that the exception is thrown inside the reader.Read() but it's handled, returning false? – Liam Oct 03 '12 at 07:39
  • @Liam: It's not just an "inside exception", that's why the application fails without the debugger attached, which is the expected behavior. Changing the break-settings in the Debug/Exceptions window doesn't change anything here. What _does_ fix the strange behavior is the debugging setting: ["Unwind the call stack on unhandled exceptions"](http://blogs.msdn.com/b/saraford/archive/2008/08/08/did-you-know-what-unwinding-the-call-stack-on-unhandled-exceptions-does-277.aspx). Unchecking this options results in expected failure of the app in debug mode, but it still doesn't answer my question. – foka Oct 03 '12 at 08:12
  • No repro at all, I didn't expect one. Pressing F5 after the exception stops the debugger could only display the console window for a fraction of a second. How could you ever tell that "Ok" got displayed? Use Console.ReadLine() instead. – Hans Passant Oct 03 '12 at 10:42
  • @Hans I've added Console.ReadKey() line – foka Oct 03 '12 at 11:37
  • You still never explained how you managed to see "Ok" in a few milliseconds. What is really going on? – Hans Passant Oct 03 '12 at 11:39
  • @Hans You're missing the point. Now, with Console.ReadKey() the question is improved and you can see "Ok", so if you can answer my question, I will be the most grateful if you do. – foka Oct 03 '12 at 11:54
  • 2
    @Hans It doesn't seem like a duplicate, does it? All the examples and references in question you gave refer to Windows Forms and `Form.Load` event. Even symptoms are different (in my case VS shows the unhandled exception, in theirs it doesn't). Although, I've tried targeting different platforms. The behavior from my question hasn't changed. – foka Oct 03 '12 at 12:33
  • It's not even close to duplicate. Different problem, different symptoms, totally different code and different solution. Great moderating work, guys, good voting. – foka Oct 05 '12 at 09:47

1 Answers1

3

I think I understand your confusion. When you debug and visual studio handles exception it stops at error line. In normal situation pressing F5 run again the same line and you are in loop of errors. But in your case you have only one exception and then VS run as nothing happened.

I think you realize now what is happening. First attempt on reader.Read() read file for xml data and move index in stream to the end of file. After you pressing F5 you run this line again and reader.Read() return false because EOF. That's it.

In normal run (without debugging) your application die on first uncatched error and nothing else is happening.

Bonus sample as a proof (paste is instead of your while loop):

try
{
    while (reader.Read()) { }
}
catch (Exception)
{
    Console.Out.WriteLine("We have excpetion, this is wrong file");
}

while (reader.Read()) { } // we have eof so we don't get exception only false 
bizon
  • 2,406
  • 2
  • 25
  • 28