What does Visual Studio 2010 debugger do with XmlException in XmlReader.Read?

I am running 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 single line text file with non-xml content.

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

What happens in debug mode in this case?

+4
source share
1 answer

I think I understand your confusion. When you debug and visualize the processing of studio descriptors, it stops at the error line. In a normal situation, pressing F5 starts the same line again and you are in the error loop. But in your case, you have only one exception, and then VS is triggered since nothing happened.

I think you understand what is happening now. The first attempt to read .Read () is to read the file for xml data and move the index to the stream at the end of the file. After you press F5, you run this line again and read. Read () returns false because EOF. What is it.

In a normal run (without debugging), your application dies on the first intact error, and nothing else happens.

Bonus sample as evidence (insert 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 
+3
source

Source: https://habr.com/ru/post/1437410/


All Articles