Error handling in contructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • balan

    Error handling in contructors

    Hi
    I am trying to allocate some memory inside a constructor.
    The contructor can be invoked in context of nested object creation
    (i.e object is member of another object ) or can be called
    explicitly.
    I want to know different ways to handle an error condition like when
    memory alloc fails.
    E.g
    <<
    Object::Object( int size){ array=new int[size] ; }

    o = new Object(100);[color=blue][color=green]
    >>[/color][/color]

    If array == NULL, then how do i make 'o' as NULL?
    or how do I check this condition?


    balan
  • Kevin Goodsell

    #2
    Re: Error handling in contructors

    balan wrote:
    [color=blue]
    > Hi
    > I am trying to allocate some memory inside a constructor.
    > The contructor can be invoked in context of nested object creation
    > (i.e object is member of another object ) or can be called
    > explicitly.
    > I want to know different ways to handle an error condition...[/color]

    Throw an exception.


    [color=blue]
    > like when
    > memory alloc fails.[/color]

    In this case, you don't even need to throw your own exception. The
    runtime system throws std::bad_alloc for you.
    [color=blue]
    > E.g
    > <<
    > Object::Object( int size){ array=new int[size] ; }
    >
    > o = new Object(100);[/color]

    This looks awful Java-ish. Why don't you just have

    Object o(100);

    ?

    Don't dynamically allocate unless you have to.
    [color=blue]
    >
    >
    > If array == NULL,[/color]

    In standard C++, that cannot happen. If the 'new' operation fails, an
    exception is thrown. Some compilers still incorrectly return 0, however.
    [color=blue]
    > then how do i make 'o' as NULL?[/color]

    Object *o;

    try
    {
    o = new Object(100);
    }
    catch (std::bad_alloc )
    {
    o = 0;
    }

    Or, using my earlier suggestion:

    try
    {
    Object o(100);
    // use o here
    }
    catch (...) // or use whatever exception you want
    {
    // handle error
    }

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • Jon Bell

      #3
      Re: Error handling in contructors

      In article <a543fd81.03091 51609.3b678370@ posting.google. com>,
      balan <balanh2002@yah oo.com> wrote:[color=blue]
      >Hi
      > I am trying to allocate some memory inside a constructor.[/color]
      [...][color=blue]
      > I want to know different ways to handle an error condition like when
      >memory alloc fails.
      >E.g
      ><<
      > Object::Object( int size){ array=new int[size] ; }
      >
      > o = new Object(100);[color=green][color=darkred]
      >>>[/color][/color]
      >
      > If array == NULL, then how do i make 'o' as NULL?
      > or how do I check this condition?[/color]

      If 'new' fails, it does not set array to NULL (at least not in standard
      C++). Instead, it throws a bad_alloc exception, which you can catch at
      some appropriate location in your program. A good C++ textbook
      should discuss exception handling, with examples; or you might find
      something useful with a Google search on "C++ exception handling".

      --
      Jon Bell <jtbellap8@pres by.edu> Presbyterian College
      Dept. of Physics and Computer Science Clinton, South Carolina USA

      Comment

      • Jonathan Mcdougall

        #4
        Re: Error handling in contructors

        >> If array == NULL,[color=blue]
        >
        >In standard C++, that cannot happen. If the 'new' operation fails, an
        >exception is thrown. Some compilers still incorrectly return 0, however.[/color]

        There is the nothrow specification defined in <new> :

        # include <new>

        int main()
        {
        int *p = new (nothrow) int;

        if ( p == 0 )
        {
        // something bad happened
        }
        }


        Jonathan

        Comment

        • fabio

          #5
          Re: Error handling in contructors

          Kevin Goodsell wrote:
          [color=blue]
          > balan wrote:
          >[color=green]
          >> Hi
          >> I am trying to allocate some memory inside a constructor.
          >> The contructor can be invoked in context of nested object creation
          >> (i.e object is member of another object ) or can be called
          >> explicitly.
          >> I want to know different ways to handle an error condition...[/color]
          >
          > Throw an exception.
          >
          > http://www.parashift.com/c++-faq-lit...html#faq-10.16
          >[color=green]
          >> like when
          >> memory alloc fails.[/color]
          >
          > In this case, you don't even need to throw your own exception. The
          > runtime system throws std::bad_alloc for you.
          >[color=green]
          >> E.g
          >> <<
          >> Object::Object( int size){ array=new int[size] ; }
          >>
          >> o = new Object(100);[/color]
          >
          > This looks awful Java-ish. Why don't you just have
          >
          > Object o(100);
          >
          > ?
          >
          > Don't dynamically allocate unless you have to.
          >[color=green]
          >>
          >>
          >> If array == NULL,[/color]
          >
          > In standard C++, that cannot happen. If the 'new' operation fails, an
          > exception is thrown. Some compilers still incorrectly return 0, however.
          >[color=green]
          >> then how do i make 'o' as NULL?[/color]
          >
          > Object *o;
          >
          > try
          > {
          > o = new Object(100);
          > }
          > catch (std::bad_alloc )
          > {
          > o = 0;
          > }
          >
          > Or, using my earlier suggestion:
          >
          > try
          > {
          > Object o(100);
          > // use o here
          > }
          > catch (...) // or use whatever exception you want
          > {
          > // handle error
          > }
          >
          > -Kevin[/color]


          what you've written in your constructor is not a good programming style if
          for example you'll also allocate some different resources.

          if you had for example:

          Object::Object( int size)
          {
          array1 = new int[size];
          array2 = new int[size];
          something_else( );
          }

          ~OObject::Objec t()
          {
          delete[] array2;
          delete[] array1;
          }

          int main()
          {
          Object instance(10);
          }

          and array2 threw an exception, the constructor would fail and the memory
          resource allocated for array1 would remain allocated, because the Object
          instance is not yet created and destructor would not be called.

          Anytime you need a resource, as written in the Stroustrup's book, make an
          initialization (for example you could there use a std::vector).

          Comment

          • balan

            #6
            Re: Error handling in contructors

            jtbellq2f@presb y.edu (Jon Bell) wrote in message news:<bk640d$j5 e$1@jtbell.pres by.edu>...[color=blue]
            > In article <a543fd81.03091 51609.3b678370@ posting.google. com>,
            > balan <balanh2002@yah oo.com> wrote:[color=green]
            > >Hi
            > > I am trying to allocate some memory inside a constructor.[/color]
            > [...][color=green]
            > > I want to know different ways to handle an error condition like when
            > >memory alloc fails.
            > >E.g
            > ><<
            > > Object::Object( int size){ array=new int[size] ; }
            > >
            > > o = new Object(100);[color=darkred]
            > >>>[/color]
            > >
            > > If array == NULL, then how do i make 'o' as NULL?
            > > or how do I check this condition?[/color]
            >
            > If 'new' fails, it does not set array to NULL (at least not in standard
            > C++). Instead, it throws a bad_alloc exception, which you can catch at
            > some appropriate location in your program. A good C++ textbook
            > should discuss exception handling, with examples; or you might find
            > something useful with a Google search on "C++ exception handling".[/color]

            THanks for all responses. I will have to read up on exception handling.

            balan

            Comment

            Working...