G
Guest
According to MSDN2, if a managed class throws an exception in the
constructor, the destructor will be called. If an exception is thrown in the
constructor the object never existed so how in the world can the destructor
of a object that does not exist get called!
Here is the MSDN2 document:
Code authored in Visual C++ and compiled with /clr will run a type's
destructor for the following reasons:
....
If an exception is thrown during the object's construction.
....
So for an exception is thrown in a "Parrot" class constructor:
"He's not pining! He's passed on! This Parrot is no more! He has ceased to
be! He's expired and gone to meet his maker! He's a stiff! Bereft of life, he
rests in peace! If you hadn't nailed him to the perch he'd be pushing up the
daisies! His metabolic processes are now history! He's off the twig! He's
kicked the bucket, he's shuffled off his mortal coil, run down the curtain
and joined the bleedin' choir invisible! This is an ex-Parrot!"
Which leads to:
Q: What does emitting an exception from a constructor mean?
A: It means that construction has failed, the object never existed, its
lifetime never began. Indeed, the only way to report the failure of
construction -- that is, the inability to correctly build a functioning
object of the given type -- is to throw an exception.
In biological terms, conception took place -- the constructor began --, but
despite best efforts it was followed by a miscarriage -- the constructor
never ran to term(ination).
Incidentally, this is why a destructor will never be called if the
constructor didn't succeed -- there's nothing to destroy. "It cannot die, for
it never lived." Note that this makes the phrase "an object whose constructor
threw an exception" really an oxymoron. Such a thing is even less than an
ex-object... it never lived, never was, never breathed its first. It is a
non-object.
So how do we rationalize calling the destructor in C++/cli when an exception
is thrown in the constructor?
constructor, the destructor will be called. If an exception is thrown in the
constructor the object never existed so how in the world can the destructor
of a object that does not exist get called!
Here is the MSDN2 document:
Code authored in Visual C++ and compiled with /clr will run a type's
destructor for the following reasons:
....
If an exception is thrown during the object's construction.
....
So for an exception is thrown in a "Parrot" class constructor:
"He's not pining! He's passed on! This Parrot is no more! He has ceased to
be! He's expired and gone to meet his maker! He's a stiff! Bereft of life, he
rests in peace! If you hadn't nailed him to the perch he'd be pushing up the
daisies! His metabolic processes are now history! He's off the twig! He's
kicked the bucket, he's shuffled off his mortal coil, run down the curtain
and joined the bleedin' choir invisible! This is an ex-Parrot!"
Which leads to:
Q: What does emitting an exception from a constructor mean?
A: It means that construction has failed, the object never existed, its
lifetime never began. Indeed, the only way to report the failure of
construction -- that is, the inability to correctly build a functioning
object of the given type -- is to throw an exception.
In biological terms, conception took place -- the constructor began --, but
despite best efforts it was followed by a miscarriage -- the constructor
never ran to term(ination).
Incidentally, this is why a destructor will never be called if the
constructor didn't succeed -- there's nothing to destroy. "It cannot die, for
it never lived." Note that this makes the phrase "an object whose constructor
threw an exception" really an oxymoron. Such a thing is even less than an
ex-object... it never lived, never was, never breathed its first. It is a
non-object.
So how do we rationalize calling the destructor in C++/cli when an exception
is thrown in the constructor?