469,648 Members | 1,557 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

is an object destroyed/closed when an exception occurs

I was wondering when you create a new xmltextreader (or any other
object for that matter), is it destroyed/closed (memory/resources
freed) when an exception occurs ?

Dim xmlrdr As New XmlTextReader(Me.Server.MapPath("staff.xml"))
Try
Do While xmlrdr.Read()
Loop
xmlrdr.Close()
lblWellFormed.Text = "Well-Formed"
Catch ex As XmlException
lblWellFormed.Text = ex.Message
End Try

I don't do anything with the data I read, is just to test if it is
well-formed XML.
When staff.xml isn't well-formed an XMLException occurs.
Are the resources then freed like in xmlrdr.Close() ?
How can I test this ?
Or should I add a Finally to the try-catch and put xmlrdr.Close() in
the finally ?

thanx,

Pugi!

Sep 4 '06 #1
7 1091
Only if the exception occurred after the xmlrdr.Close() statement. If it
occurs before that, it's left open. To ensure xmlrdr is always closed use
the following:

Dim xmlrdr As New XmlTextReader(Me.Server.MapPath("staff.xml"))
Try
Do While xmlrdr.Read()
Loop
lblWellFormed.Text = "Well-Formed"

Catch ex As XmlException
lblWellFormed.Text = ex.Message

Finally
xmlrdr.Close()
End Try

Mike Ober.
<pu******@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
I was wondering when you create a new xmltextreader (or any other
object for that matter), is it destroyed/closed (memory/resources
freed) when an exception occurs ?

Dim xmlrdr As New XmlTextReader(Me.Server.MapPath("staff.xml"))
Try
Do While xmlrdr.Read()
Loop
xmlrdr.Close()
lblWellFormed.Text = "Well-Formed"
Catch ex As XmlException
lblWellFormed.Text = ex.Message
End Try

I don't do anything with the data I read, is just to test if it is
well-formed XML.
When staff.xml isn't well-formed an XMLException occurs.
Are the resources then freed like in xmlrdr.Close() ?
How can I test this ?
Or should I add a Finally to the try-catch and put xmlrdr.Close() in
the finally ?

thanx,

Pugi!



Sep 4 '06 #2

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:ic****************@newsread4.news.pas.earthli nk.net...
Only if the exception occurred after the xmlrdr.Close() statement. If it
occurs before that, it's left open. To ensure xmlrdr is always closed use
the following:

Dim xmlrdr As New XmlTextReader(Me.Server.MapPath("staff.xml"))
Try
Do While xmlrdr.Read()
Loop
lblWellFormed.Text = "Well-Formed"

Catch ex As XmlException
lblWellFormed.Text = ex.Message

Finally
xmlrdr.Close()
End Try

Mike Ober.

True but unfortunately you have opened the reader outside of the try/catch
and it's constructor could 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

Sep 4 '06 #3
Good point - it didn't occur to me to check for the constructor throwing an
exception.

Mike.

"Robinson" <it*****************@nowmyinboxisfull.comwrote in message
news:ed*******************@news.demon.co.uk...
>
"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:ic****************@newsread4.news.pas.earthli nk.net...
Only if the exception occurred after the xmlrdr.Close() statement. If
it
occurs before that, it's left open. To ensure xmlrdr is always closed
use
the following:

Dim xmlrdr As New XmlTextReader(Me.Server.MapPath("staff.xml"))
Try
Do While xmlrdr.Read()
Loop
lblWellFormed.Text = "Well-Formed"

Catch ex As XmlException
lblWellFormed.Text = ex.Message

Finally
xmlrdr.Close()
End Try

Mike Ober.


True but unfortunately you have opened the reader outside of the try/catch
and it's constructor could 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



Sep 4 '06 #4
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
Sep 4 '06 #5

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!

Sep 5 '06 #6

pugin...@gmail.com schreef:
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!
Sorry, the above code will work nicely. But the moment I place code in
the try-block before the statement 'xmlrdr = New
XmlTextReader(Me.Server.MapPath("staff.xml"))' and an exception occurs
before the mentioned statment, closing the reader throws a new
exception in the finally block.

I think this code should do it :

Dim xmlrdr As XmlTextReader = Nothing
Try
' if some code here throws an exception before next
statement is executed, xmlrdr Is Nothing
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
If Not (xmlrdr Is Nothing) Then
xmlrdr.Close()
End If
End Try

I guess this wraps it up, unless anyone has some suggestions ?

Thanx for the replies.

Pugi!

Sep 5 '06 #7

pu******@gmail.com wrote:
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.
Nope. If the constructor throws, then the object is nothing. The code
prevents a null reference exception in the finally block.

Module Module1

Sub Main()
Dim a As ThrowAnException
Try
a = New ThrowAnException()
Catch ex As Exception
If a Is Nothing Then Console.WriteLine("It's nothing!")
Console.WriteLine(ex.Message)
End Try
End Sub

Class ThrowAnException
Public Sub New()
Throw New Exception("We threw!")
End Sub
End Class
End Module

HTH,

--
Tom Shelton

Sep 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by John Marshall | last post: by
1 post views Thread by Roman S. Golubin1709176985 | last post: by
8 posts views Thread by MV | last post: by
reply views Thread by John Manion via .NET 247 | last post: by
2 posts views Thread by Marco | last post: by
5 posts views Thread by Michael Moreno | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.