how to handle the exception handling when NEW operator failed to allocate memory

Discussion in 'C++' started by jayapal, Jul 17, 2007.

  1. jayapal

    jayapal Guest

    Hi ,

    I am using the NEW operator to allocate the memory in many places of
    my code.But I am not doing any error hadling or exception handling.Can
    any one suggests me how to do exception handling, which code part I
    have to add to do the exception handling

    Thanks in advance,

    ..
     
    jayapal, Jul 17, 2007
    #1
    1. Advertising

  2. jayapal wrote:
    > I am using the NEW operator


    I believe you mean the "new operator" (C++ is case sensitive).

    > to allocate the memory in many places of
    > my code.But I am not doing any error hadling or exception handling.Can
    > any one suggests me how to do exception handling, which code part I
    > have to add to do the exception handling


    What book on C++ are you reading that doesn't give any "suggestions"?

    Error handling and exceptions are not the same thing nor they have the
    same purpose. Books have been written to try to explain those concepts
    and not many actually succeed, unfortunately. Start with chapter 14
    in TC++PL. Then ask more question if you get any.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 17, 2007
    #2
    1. Advertising

  3. jayapal

    Kai-Uwe Bux Guest

    jayapal wrote:

    > Hi ,
    >
    > I am using the NEW operator to allocate the memory in many places of
    > my code.But I am not doing any error hadling or exception handling.Can
    > any one suggests me how to do exception handling, which code part I
    > have to add to do the exception handling


    Exception handling is done by adding try blocks, catch handlers, and throw
    statements to your code. Where to put them depends on what you want to
    achieve. Some throw statements may allready be in your code hidden in calls
    to library functions that might throw.

    The interaction of try-throw-catch and manually managed pointers is tricky.
    Since every new() is to be matched with one and only one corresponding
    delete() along _each execution path_, try-throw-catch makes manual pointer
    management much harder. Any line that could throw can divert the flow
    control to some catch handler; any pointer that undergoes stack unwinding
    may leak memory. E.g.:

    try {
    some_type * some_ptr ( new some_type ( some parameters ) );
    some_ptr->mem_fct(); // if this line throws, delete will not be called.
    delete some_ptr;
    }
    catch ( ... ) {
    // here the some_ptr object is irretrievably lost.
    // memory has leaked
    }

    Note that it is very hard to tell which lines may throw. In particular when
    templates enter the picture, the type "some_type" in the above snippet may
    be a user provided type. In that case, all bets are off as to whether
    mem_fct() could throw. The only safe attitude is: each line can throw and
    flow control may go some ((possibly unknown) place at any given moment.

    Various idioms can be used to ensure that every new() is matched with a
    corresponding delete(). You may want to read up on RAII and on smart
    pointers.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jul 17, 2007
    #3
  4. jayapal

    Guest

    On Jul 17, 5:16 pm, "Victor Bazarov" <> wrote:
    > jayapal wrote:
    > > I am using the NEW operator

    >
    > I believe you mean the "new operator" (C++ is case sensitive).
    >
    > > to allocate the memory in many places of
    > > my code.But I am not doing any error hadling or exception handling.Can
    > > any one suggests me how to do exception handling, which code part I
    > > have to add to do the exception handling

    >
    > What book on C++ are you reading that doesn't give any "suggestions"?
    >
    > Error handling and exceptions are not the same thing nor they have the
    > same purpose. Books have been written to try to explain those concepts
    > and not many actually succeed, unfortunately. Start with chapter 14
    > in TC++PL. Then ask more question if you get any.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask




    Hi,

    I worked too hard to find the solution and found that this is a
    standard exception which is thrown by new itself.



    // bad_alloc standard exception
    #include <iostream>
    #include <exception>
    using namespace std;

    int main () {
    try
    {
    int* myarray= new int[1000];
    }
    catch (exception& e)
    {
    cout << "Standard exception: " << e.what() << endl;
    }
    return 0;
    }





    --
    Regards
    Mayank Jain
    Niksun
    9818390836
    www.mayankjain.110mb.com
     
    , Jul 18, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Gifford
    Replies:
    4
    Views:
    394
    tom_usenet
    Oct 27, 2003
  2. Bryan Parkoff

    new -- allocate memory question

    Bryan Parkoff, Apr 5, 2005, in forum: C++
    Replies:
    1
    Views:
    423
    Alf P. Steinbach
    Apr 5, 2005
  3. Replies:
    2
    Views:
    324
    benben
    Nov 26, 2006
  4. jayapal
    Replies:
    1
    Views:
    379
    James Kanze
    Jul 18, 2007
  5. firejdl
    Replies:
    0
    Views:
    151
    firejdl
    Mar 22, 2006
Loading...

Share This Page