473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1168
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Marshall | last post by:
Hi, Does anyone see a problem with doing: data = file("tata").read() Each time this is done, I see a new file descriptor allocated (Linux) but not released. 1) Will there ever be a point...
2
by: CoolPint | last post by:
Standard exception classes in C++ have what() which returns const char * and they have constructors accepting string. Where is that string created and when is the string destroyed? In the...
1
by: Roman S. Golubin1709176985 | last post by:
Hello everybody! I wrote an project with one class (Class) and two forms (Form1, Form2): -----------------------------------Form2AndClass1Begin---------------------- ------------------ public...
1
by: Robert A Riedel | last post by:
I am completely baffled when the following managed exception is thrown: "Object reference not set to an instance of an object" from a nested subroutine when referencing a variable allocated on the...
8
by: MV | last post by:
Why can't I run .Show() on a System.Windows.Forms object from a Class? The code works fine if i run it from an other form. The form gets visible but no controls are loaded and the mouse pointer...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
2
by: Marco | last post by:
Hello, I have to use the propertyReader of DSOFile.dll for reading/writing custom document properties in a closed document. In the msdn http://support.microsoft.com/?scid=kb;en-us;Q224351...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
7
by: Brad | last post by:
I have several web apps which download content to client (pdf, tif, etc) by writing out content in an aspx page. I've recently upgraded these from 1.1 to 2.0....and they all seem to work fine...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.