Re: is such exception handling approach good?
- From: ajk <ak@xxxxxxxxxxxx>
- Date: Sat, 22 Dec 2007 14:33:14 +0800
On Fri, 21 Dec 2007 17:36:15 +0200, "Alex Blekhman"
<tkfx.REMOVE@xxxxxxxxx> wrote:
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
I think there are several reasons why one shouldn't have too much code
in the ctor.
One reason is that the state of the object is not defined. If you
instead have the functionality in other member functions you have more
control on the state of the object and better handle the error
conditions and cleanup.
Say if a class initializes some memory, grabs some resource and opens
an oracle database in the ctor then what should you do with the object
if one of those three failed? IMO it is not a good design to stuff all
this initialization in the ctor.
However if you instead have split up the initialization in three
functions you are more clear how to handle failure of any of these
three and whether or not you should proceed. The code gets clearer for
somebody else to read it.
It gets even worse if the class with the complex initialization has
been derived from some baseclass - if one of the base classes throws
an exception.
.
- Follow-Ups:
- Re: is such exception handling approach good?
- From: Alex Blekhman
- Re: is such exception handling approach good?
- From: Abhishek Padmanabh
- 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: Alex Blekhman
- is such exception handling approach good?
- Prev by Date: Re: How to debug "new operator" crash ?
- 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
|
Loading