Re: is such exception handling approach good?



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.


.



Relevant Pages

  • ECMA Wrong - Class and Object Initialization Rules - Help!
    ... ECMA states in Section 1.8.1.4 on Class and Object ... Initialization Rules: ... "An object constructor shall not return unless a constructor for the base ... if a .ctor didn't invoke a base class .ctor ...
    (microsoft.public.dotnet.framework.clr)
  • Re: ECMA Wrong - Class and Object Initialization Rules - Help!
    ... 'Initialization' in this context has only to do with the state of the ... calling the base class constructor. ... > This means in particular that, if a .ctor didn't invoke a base class .ctor ...
    (microsoft.public.dotnet.framework.clr)
  • RE: ECMA Wrong - Class and Object Initialization Rules - Help!
    ... An instance of a class can be created only if the constructor of the class' ... > Initialization Rules: ... > pointer as uninitialized unless the base class constructor has been called." ... > This means in particular that, if a .ctor didn't invoke a base class .ctor ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Message Builder vs. a Build Method?
    ... Builder class would encapsulate all the complex algorithms for making ... OTOH, as Daniel T. suggests, sometimes the initialization requires unique processing for initialization that is clearly intrinsic to the object itself. ... Constructors tend to be fragile and it is difficult to manage errors when they occur in a constructor scope, so it is usually a good idea to keep the processing in constructors as simple as possible. ... When the initialization of attribute data requires complex processing AND it seems like is intrinsic ot the object, that justifies having a separate initialization method that is invoked immediately after the constructor. ...
    (comp.object)
  • Re: Constructor as a "Reset" Button
    ... constructor is more or less just for initialization, ... In PHP you might get ... And HIPPA certification is not easy - nor is it cheap. ...
    (comp.lang.php)

Loading