Re: is such exception handling approach good?



On Dec 22, 12:34 pm, Abhishek Padmanabh <abhishek.padman...@xxxxxxxxx>
wrote:
On Dec 22, 11:33 am, ajk <a...@xxxxxxxxxxxx> wrote:
On Fri, 21 Dec 2007 17:36:15 +0200, "Alex Blekhman"

<tkfx.REM...@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.

It may be a good design, it may not be a good design from user's point
of view. The user might want to have a class wherein, by default he
doesn't get the connection created automatically on object creation.
But if it makes sense for the user of the class that those many
processings must be done automatically upon object creation, then from
C++ point of view it is achievable, with clarity and with stability.

You can use some sort of a smart pointer that does automatic cleanup
on stack unwinding if exception is raised. You can call the release
resource API to free-up the resource on exception. You can close the
connection if something else in the constructor threw after opening
it. These all can be based on RAII and you would not need to do
anything specific for exception handling yourself. Here:

MyClass::MyClass()
{
       //could wrap the logic inside a private member if the
constructor code would be shared...
       shared_ptr<someclass> shptr(new someclass());
       mem_shared_ptr = shptr;
       //GrabAResource();
       try{
          OpenConnToDB();
       }catch(exception& ex) //whatever specific exception catch
       {
           //ReleaseAResource();

throw ex; //or anyother custom exception as
appropriate

       }
}

Sorry, should have rethrown the exception...
.



Relevant Pages

  • Re: is such exception handling approach good?
    ... It may be a good design, it may not be a good design from user's point ... doesn't get the connection created automatically on object creation. ... instance you use a class somebody else wrote which throws an exception ... constructor code would be shared... ...
    (microsoft.public.vc.language)
  • RE: inheritance trouble - how to detect run-time or design-time in code
    ... The base class will throw a exception its ... VS.NET's design view again, yes? ... However, as for the Constructor, we're unable to use the above means to ... Move the code into page's OnInit method and use the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: exiting a constructor early
    ... the exception object, and to 're-throw' a different object if you ... the body of the constructor - the handler is for handling the ... exception object - the constructor body need not do it. ... | at your design and ask yourself why that is the case. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: DbC & Exceptions & Style
    ... exception. ... Different ways the constructor can fail ... to avoid writing code that converts GUI data into objects, ... All domain objects must now provide a default constructor. ...
    (comp.object)
  • Re: Controlled types and exception safety
    ... >> propagate an exception. ... >> For an Adjust invoked as part of an assignment operation, ... But a user-defined constructor is ... a user-defined constructor has just turned on the ...
    (comp.lang.ada)

Quantcast