Re: is such exception handling approach good?
- From: "Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx>
- Date: Fri, 21 Dec 2007 17:36:15 +0200
"ajk" 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.
I beg to differ. It is exactly the opposite. Splitting
object's construction into two parts introduces unnecessary
and dangerous period when object's state is undetermined.
There is nothing wrong with throwing from constructor. You
won't end up with invalid object. You end up with valid
working object if the constructor succeeds, or no object at
all, otherwise.
Alex
.
- Follow-Ups:
- References:
- is such exception handling approach good?
- From: George
- Re: is such exception handling approach good?
- From: ajk
- is such exception handling approach good?
- Prev by Date: Re: is such exception handling approach good?
- Next by Date: Re: is such exception handling approach good?
- Previous by thread: Re: is such exception handling approach good?
- Next by thread: Re: is such exception handling approach good?
- Index(es):
Relevant Pages
|