Re: is such exception handling approach good?
- From: ajk <ak@xxxxxxxxxxxx>
- Date: Sun, 23 Dec 2007 08:39:18 +0800
On Sat, 22 Dec 2007 16:11:04 -0500, "Larry Smith"
<no_spam@xxxxxxxxxxx> wrote:
it is normally not a good idea to do operations in the ctor that could
cause an exception. the reason for this is that if an exception is
ever thrown in your ctor you end up with an invalid object. therefore
it is better to have some other method to initialize such with and
handle a failure outside the ctor like bool init() or something.
That's completely false as Alex stated.
i forgot to add: the dtor is never called when ctor throws an
exception so the heck do you do the cleanup if the ctor has allocated
three different things?
please tell me since you obviously have such insight.
The canonical example:
class MyClass
{
public:
MyClass() : m_Whatever(new Whatever)
{
// m_Whatever's destructor is still called
throw SomeException();
}
private:
std::auto_ptr<Whatever> m_Whatever;
};
so your requirement is that all member variables must be smart
pointers? not that great.
.
- Follow-Ups:
- Re: is such exception handling approach good?
- From: George
- Re: is such exception handling approach good?
- From: Larry Smith
- Re: is such exception handling approach good?
- References:
- is such exception handling approach good?
- From: George
- Re: is such exception handling approach good?
- From: ajk
- Re: is such exception handling approach good?
- From: Larry Smith
- Re: is such exception handling approach good?
- From: ajk
- Re: is such exception handling approach good?
- From: Larry Smith
- is such exception handling approach good?
- Prev by Date: Re: is such exception handling approach good?
- Next by Date: Re: Passing pointers?
- Previous by thread: Re: is such exception handling approach good?
- Next by thread: Re: is such exception handling approach good?
- Index(es):
Relevant Pages
|