I am investigating some cases in my application. I have this code to read XML elements with the goal to skip those elements in which exception is triggered:
我正在调查我申请中的一些案件。我有这个代码来读取XML元素,目的是跳过触发异常的元素:
bool res = true;
// Start parsing the XML file
using (XmlReader reader = XmlReader.Create(xmlFilePath))
{
while (res)
{
try
{
// Read
res = reader.Read();
if (res == false)
break;
// Is this XML element?
if ((reader.NodeType == XmlNodeType.Element))
{
// What is its name?
if (reader.Name == "Test")
{
//...
}
}
}
catch (Exception ex)
{
// Exception - continue to reading other XML elements
HelperMethods.AppendToLogFile("Method: TryRetrieveRestartReasons. " + terminalName + " " + "Filename: " + xmlFilePath + "Ex: " + ex.Message, LogType.Error);
// HelperMethods.TryInsertParsingExceptionToDatabase(terminalName, Path.GetFileNameWithoutExtension(xmlFilePath) + ".log", ex.Message);
}
}
}
My question is do you think above snippet can ever result in infinite loop? Otherwise how would you proceed to write code where you skip those lines which generated exception in XML file
我的问题是你认为上面的片段会导致无限循环吗?否则你将如何继续编写代码,跳过那些在XML文件中生成异常的行
2 个解决方案
#1
Consider the scenario where reader.Read()
reads successfully once, then throws exception for every time after that.
考虑一下reader.Read()成功读取一次的情况,然后每次都抛出异常。
The first time the line res = reader.Read()
gets called, res
gets set to true
. Then, each time afterwards reader.Read()
throws an exception. The key part is that when it throws an exception nothing happens to res
. It retains its value of true
.
第一次调用res = reader.Read()时,res被设置为true。然后,每次读取时,reader.Read()都会抛出异常。关键部分是,当它抛出一个异常时,res没有任何反应。它保留了它的真实价值。
So, you catch the exception, res
stays true
, and the loop starts over. The while
says "Yep, res
is true
. Keep going." Then, another exception is thrown before res
can be set to anything else.
因此,您捕获异常,res保持为true,循环重新开始。同时说“是的,res是真的。继续前进。”然后,在将res设置为其他任何内容之前,抛出另一个异常。
So, so long as it is reader.Read()
throwing an exception and you have had at least one successful Read()
call, then you will get an infinite loop.
所以,只要它是reader.Read()抛出异常并且你至少有一次成功的Read()调用,那么你将得到一个无限循环。
#2
I need to know answer from someone who is sure if reader.Read() will always return false in my above code
我需要知道一个确定reader.Read()将始终在上面的代码中返回false的人的回答
It will not always return false
.
Since XmlReader.Read
can throw exceptions (e.g., when XML is invalid), then here is a problem with try-catch
and reader.Read
.
它不会总是返回false。由于XmlReader.Read可以抛出异常(例如,当XML无效时),因此这是try-catch和reader.Read的问题。
If Read
throws exception, then t
never becomes false
, and you'll get infinite loop. First line of the loop should be while (reader.Read())
.
如果Read抛出异常,则t永远不会变为false,并且您将获得无限循环。循环的第一行应该是while(reader.Read())。
the next time I called Reader.Read -- it returned FALSE
下次我调用Reader.Read - 它返回FALSE
You can't be sure. Even if this is true for now/for some XML samples, this is implementation detail. This behavior isn't documented. I wouldn't rely on it.
你不能确定。即使现在/某些XML示例都是如此,这也是实现细节。此行为未记录。我不会依赖它。
#1
Consider the scenario where reader.Read()
reads successfully once, then throws exception for every time after that.
考虑一下reader.Read()成功读取一次的情况,然后每次都抛出异常。
The first time the line res = reader.Read()
gets called, res
gets set to true
. Then, each time afterwards reader.Read()
throws an exception. The key part is that when it throws an exception nothing happens to res
. It retains its value of true
.
第一次调用res = reader.Read()时,res被设置为true。然后,每次读取时,reader.Read()都会抛出异常。关键部分是,当它抛出一个异常时,res没有任何反应。它保留了它的真实价值。
So, you catch the exception, res
stays true
, and the loop starts over. The while
says "Yep, res
is true
. Keep going." Then, another exception is thrown before res
can be set to anything else.
因此,您捕获异常,res保持为true,循环重新开始。同时说“是的,res是真的。继续前进。”然后,在将res设置为其他任何内容之前,抛出另一个异常。
So, so long as it is reader.Read()
throwing an exception and you have had at least one successful Read()
call, then you will get an infinite loop.
所以,只要它是reader.Read()抛出异常并且你至少有一次成功的Read()调用,那么你将得到一个无限循环。
#2
I need to know answer from someone who is sure if reader.Read() will always return false in my above code
我需要知道一个确定reader.Read()将始终在上面的代码中返回false的人的回答
It will not always return false
.
Since XmlReader.Read
can throw exceptions (e.g., when XML is invalid), then here is a problem with try-catch
and reader.Read
.
它不会总是返回false。由于XmlReader.Read可以抛出异常(例如,当XML无效时),因此这是try-catch和reader.Read的问题。
If Read
throws exception, then t
never becomes false
, and you'll get infinite loop. First line of the loop should be while (reader.Read())
.
如果Read抛出异常,则t永远不会变为false,并且您将获得无限循环。循环的第一行应该是while(reader.Read())。
the next time I called Reader.Read -- it returned FALSE
下次我调用Reader.Read - 它返回FALSE
You can't be sure. Even if this is true for now/for some XML samples, this is implementation detail. This behavior isn't documented. I wouldn't rely on it.
你不能确定。即使现在/某些XML示例都是如此,这也是实现细节。此行为未记录。我不会依赖它。