 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rohit Dhamija Guest
|
Posted: Mon Aug 23, 2004 10:33 am Post subject: Result of Exception in constructor and destructor !! |
|
|
Dear All,
I had following query:
What happens if an exception occurs inside a constructor and in case
of destructor ? I mean to say how does C++ complier takes care of such
exceptions.
In case, if exception occurs inside a constructor :
1)Does the object of corresponding class gets created ?
2)Or desctructor gets called ?
Please comment !
Thanks & Regards,
Rohit Dhamija
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stefan Heinzmann Guest
|
Posted: Mon Aug 23, 2004 10:13 pm Post subject: Re: Result of Exception in constructor and destructor !! |
|
|
Rohit Dhamija wrote:
| Quote: | Dear All,
I had following query:
What happens if an exception occurs inside a constructor and in case
of destructor ? I mean to say how does C++ complier takes care of such
exceptions.
In case, if exception occurs inside a constructor :
1)Does the object of corresponding class gets created ?
2)Or desctructor gets called ?
|
If the exception leaves the constructor (i.e. it is not caught inside
the constructor), the object is not constructed, therefore it does not
get destructed either. Think as if the object had never existed - a
miscarriage. The object's lifetime only begins when the constructor
completes in the normal way.
If some of the object's members have been constructed successfully at
the point in time when the exception is thrown, their destructors are
called automatically.
It is a design guideline to never allow an exception to leave
destructors. See for example Herb Sutter's two books about Exceptional
C++. You may also want to read Appendix E of Stroustrup's book "The C++
Programming Language 3rd Ed.", available online here:
http://www.research.att.com/~bs/3rd_safe.pdf
--
Cheers
Stefan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Hopkin Guest
|
Posted: Mon Aug 23, 2004 10:41 pm Post subject: Re: Result of Exception in constructor and destructor !! |
|
|
[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0408222013.566268b7 (AT) posting (DOT) google.com>...
| Quote: |
What happens if an exception occurs inside a constructor and in case
of destructor ? I mean to say how does C++ complier takes care of such
exceptions.
|
If an exception is propagated from a constructor (i.e. isn't caught),
it's as if the object never existed. Therefore, the destructor doesn't
get called.
It's considered *very* bad style to allow destructors to emit
exceptions. For one thing, classes with destructors that throw are not
allowed to be stored in standard containers.
This article gave me a good handle on exception safety (and it answers
your question better than I have)
<http://www.cs.huji.ac.il/course/2003/ood/resources/Exception_Safe_Generic_Containers.pdf>
Mr. Sutter covers similar material, and much else, in "Exceptional
C++" and his Guru of the Week website.
James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Bell Guest
|
Posted: Mon Aug 23, 2004 11:03 pm Post subject: Re: Result of Exception in constructor and destructor !! |
|
|
[email]rohit_dhamija (AT) rediffmail (DOT) com[/email] (Rohit Dhamija) wrote in message news:<e77b983b.0408222013.566268b7 (AT) posting (DOT) google.com>...
| Quote: | Dear All,
I had following query:
What happens if an exception occurs inside a constructor and in case
of destructor ? I mean to say how does C++ complier takes care of such
exceptions.
In case, if exception occurs inside a constructor :
1)Does the object of corresponding class gets created ?
2)Or desctructor gets called ?
Please comment !
|
If a constructor throws, the corresponding destructor is not called.
However, the destructors of all constructed members, as well as the
destructor of the base class(es) is called. Example:
class Foo : public Bar {
public:
Foo();
~Foo();
private:
Baz mBaz;
};
Foo::Foo()
{
throw 0;
}
First, ~Baz() gets called to destroy mBaz, then ~Bar() gets called to
destroy the Bar base class part of the Foo. Because the Foo
constructor never finished, the object is not considered as having
ever existed, therefore ~Foo() is not called.
As for your second question, you should never throw an exception from
a destructor, because the destructor can be called during stack
unwinding for another exception. If such a destructor throws, the
second exception causes std::terminate to be called.
Bob
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|