Andrew Morton schreef:
Robinson wrote:
True but unfortunately you have opened the reader outside of the
try/catch and it's constructor could throw..... ;)
But then surely trying to .close() the XML reader would throw?
Dim xmlrdr As XmlTextReader
Try
xmlrdr = new XmlTextReader (Me.Server.MapPath("staff.xml"))
Do While xmlrdr.Read()
Loop
lblWellFormed.Text = "Well-Formed"
Catch ex As XmlException
lblWellFormed.Text = ex.Message
Finally
xmlrdr.Close()
End Try
So wouldn't it need
Finally
If Not (xmlrdr Is Nothing) Then
xmlrdr.Close()
End If
End Try
?
Andrew
Nice, but the moment the code 'Dim xmlrdr As XmlTextReader' gets
executed, which is allways, xmlrdr is 'something', so Not (xmlrdr Is
Nothing) will allways return true. So the if statement in the finally
block is useless. Unless there is a way to test if any resources have
been allocated to this object. Is Nothing test wether an object exists,
not for allocated resources.
If the file doesn't exist, it will throw an exception different from
XmlException. So for the moment, the code looks like this:
Dim xmlrdr As XmlTextReader
Try
xmlrdr = New XmlTextReader(Me.Server.MapPath("staff.xml"))
Do While xmlrdr.Read
Loop
lblWellFormed.Text = "Well-Formed"
Catch ex As XmlException
lblWellFormed.Text = "XML : " & ex.Message
Catch ex As Exception
lblWellFormed.Text = "Other : " & ex.Message
Finally
xmlrdr.Close()
End Try
In the case that staff.xml does not exist, the other exception catches
it, and closing xmlrdr ,while no resources have been allocated, doesn't
result in a new exception.
Pugi!