January 1, 2006

Small managed question...

I have a small question…

Will the destructor be called in a managed class if the constructor has thrown an exception?

Here is the example (.NET 1.1; but you can also try this with .NET 2.0):

  namespace MyNamespace {

__gc class MyTest { public: MyTest() { throw new System::ApplicationException(S"Exception in the constructor"); } ~MyTest() { Console::WriteLine(S"Destructor was called"); } };
}
int _tmain() { try { MyNamespace::MyTest *mt = new MyNamespace::MyTest(); } catch(System::ApplicationException *exp) { }
}

By the way: Happy new year!


Posted 6 days, 3 hours ago on January 1, 2006
The trackback url for this post is http://blog.kalmbachnet.de/bblog/trackback.php/60/

Re: Small managed question...
I hope not!
Posted 5 days, 5 hours ago by Okko Willeboordse • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/60/349/
Re: Small managed question...
You lost!!! Managed objects are fully initialized before the constructor is called. So if you throw an exception in the constructor, the object needs to be destroyed. And thefore the descructor (or better Finalizer which is the same) must be called...
Posted 4 days, 22 hours ago by Jochen Kalmbach • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/60/350/
Re: Small managed question...
God help us. So many things to remember. Is technology here to serve us or is it the other way around?
Posted 4 days, 9 hours ago by Agnel CJ Kurian • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/60/351/
Re: Small managed question...
See http://msdn2.microsoft.com/en-us/library/ms177197.aspx Code authored in Visual C++ and compiled with /clr will run a type's destructor for the following reasons: [SNIP] If an exception is thrown during the object's construction. [SNIP] So in a managed destructor one can not be sure if constructor ran okay. So,.. So,.. I think C++ coders should know about this when moving to managed C++.
Posted 4 days, 1 hour ago by Okko Willeboordse • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/60/352/
Re: Small managed question...
Now I'm confused... I just put a little C++/CLI example together to demonstrate the destructor being called even if the constructor doesn't complete due to an exception being thrown (http://www.lenholgate.com/archives/000613.html) and it doesn't do what I expected and what the document that Okko references (http://msdn2.microsoft.com/en-us/library/ms177197.aspx) suggests that it should do. I.e. the finalizer is called but not the destructor... Am I being dense here?
Posted 1 year, 11 months ago by Len Holgate • • wwwReply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/60/353/
Re: Small managed question...
See answer to your posting: http://www.lenholgate.com/archives/000613.html
Posted 1 year, 11 months ago by Jochen Kalmbach • • • Reply
Comment Trackback URL : http://blog.kalmbachnet.de/bblog/trackback.php/60/354/

Comments have now been turned off for this post