David Williams wrote:

> Basic level question maybe, but I can find no other place to ask.

:)

> What happens when an exception is thrown in a constructor (New() for us
> VB'ers)? Is the class created?

It is in mid-creation. The resulting code that is attempting to create it,
though, will not see it. No assignment happens, because the exception is
thrown.

> More importantly, are any resources that might have been started prior to
> the exception free'ed?

Memory will be garbage collected. Other things will need to be cleaned up.

> In particular to my cases, if IDisposable is being Implement'ed does
> Dispose get called if the class instance is not actually created?

No, it won't be called. If you are allocating non-memory resources in your
constructor, and you either might throw an exception or some other class
you're using might throw one, you need to play for that.

If you're the one throwing, then you can simply clean up things before
throwing.

If you're being "thrown through" (new word? :), then you'll need to catch
said exception, clean up, and re-throw it (or perhaps throw a new, more
appropriate exception).

Brad

--
Read my web log at http://www.quality.nu/dotnetguy/

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to