473,326 Members | 2,023 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,326 software developers and data experts.

Thrown application exception bypasses class deconstructor?

Hello All

For reference

MyClass myClass = new MyClass()
myClass.MyMethod()

It appears that if you throw an application exception in MyMethod(), the myClass deconstructor will be bypassed. Can anyone confirm this please? I have a database layer class that disposes connections in the deconstructor, and I suspect the connections are staying open when exceptions occur elsewhere in the class..

Thanks

Doug

(note backwards email address!)
Nov 16 '05 #1
2 3010
"Doug Wiley" <moc.oohay@yeliwguod> wrote:
It appears that if you throw an application
exception in MyMethod(), the myClass
deconstructor will be bypassed.
[...]
I have a database layer class that disposes
connections in the deconstructor


Destructors are not guaranteed to be called in C#. Consider implementing the
IDisposable interface in your class instead..

P.
Nov 16 '05 #2
Doug Wiley <moc.oohay@yeliwguod> wrote:
For reference:

MyClass myClass = new MyClass();
myClass.MyMethod();

It appears that if you throw an application exception in MyMethod(),
the myClass deconstructor will be bypassed. Can anyone confirm this
please? I have a database layer class that disposes connections in
the deconstructor, and I suspect the connections are staying open
when exceptions occur elsewhere in the class...


No, exceptions won't cause finalizers to be bypassed. However, you
should really be using the IDisposable interface and *always* calling
Dispose appropriately. Finalizers are non-deterministic in terms of
when they'll be called (and even if, depending on how long other
finalizers take to execute at the end of an application).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Doug Wiley | last post by:
Hello All For reference MyClass myClass = new MyClass() myClass.MyMethod() It appears that if you throw an application exception in MyMethod(), the myClass deconstructor will be bypassed....
10
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
2
by: Emil Astrom | last post by:
Hi! I wonder if there's a way to retrieve information about thrown, not yet handled exceptions. My situation is similar to the code below: class MySession : IDisposable { : : Dispose()
0
by: Jon | last post by:
I have a reproducible problem. In my application I have handlers for both the AppDomain.CurrentDomain.UnhandledException event and the Applciation.ThreadException events. Up until now these...
1
by: TrtnJohn | last post by:
Is there anyway to tell from inside an exception class when it is thrown? I'd like to add logging to my application whenever a certain exception is thrown. I don't want to have to add the logging...
1
by: Chris Stiefeling | last post by:
Hi, I am experiencing a strange problem. I am reading and writing xml files via XmlDocument and XmlTextWriter. In the debugger everything works fine but outside the debugger I receive the...
4
by: Michael Rodriguez | last post by:
I have a data layer in a dll class. If I manually throw an exception in that data layer, the generic Application.OnThreadException in my UI does not catch it nor does it display any message...
0
by: Pieter | last post by:
Hi, When using clickOnce for a VB.NET 2.0 application it installs fine on every computer, except one (a new one...). Every is isstalled, Framework, Mdac, .... The error in the log file is:...
4
by: Terry Olsen | last post by:
I have a class where I throw a new exception from a method My calling code (in a different class) is wrapped in a Try/Catch block, but it's not catching. Instead the IDE is highlighting the...
1
by: Ajb181 | last post by:
hi all. i have a template class that i can not link. there is no problem when i add the method def's inside the header file but as soon as i move them out i get a link error where i define the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.