473,396 Members | 1,927 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,396 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 3017
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.