Proper Way to catch exception thrown by new keyword

Discussion in 'C++' started by Fao, Sean, Jul 5, 2003.

  1. Fao, Sean

    Fao, Sean Guest

    I'm reading The C++ Programming Language by Bjarne Stroustrup and I
    was unclear as to the proper way to catch an exception thrown by the
    new keyword. I was wondering if someobody here could let me know if I
    have written the following code correctly.

    <CODE_SNIP>
    #include <iostream>
    #include <new>

    using std::cerr;
    using std::bad_alloc;

    int main()
    {
    char *p;

    try
    {
    for(; ;)
    p = new char[100000];
    }

    catch(bad_alloc)
    {
    cerr << "Error allocating memory...\n";
    return 1;
    }

    delete p;

    return 0;
    }

    </CODE_SNIP>

    I thank you in advance for any pointers you can provide.

    Sean
    Fao, Sean, Jul 5, 2003
    #1
    1. Advertising

  2. "Fao, Sean" <> wrote in message
    news:...
    > I'm reading The C++ Programming Language by Bjarne Stroustrup and I
    > was unclear as to the proper way to catch an exception thrown by the
    > new keyword. I was wondering if someobody here could let me know if I
    > have written the following code correctly.


    Well that depends on what you want it to do.

    >
    > <CODE_SNIP>
    > #include <iostream>
    > #include <new>
    >
    > using std::cerr;
    > using std::bad_alloc;
    >
    > int main()
    > {
    > char *p;
    >
    > try
    > {
    > for(; ;)
    > p = new char[100000];


    Are you trying to loop until you run out of memory?

    If two allocations succeed then this loop will leak memory. Because the
    second allocation will overwrite the first and you will never be able to
    free that memory.

    > }
    >
    > catch(bad_alloc)


    This isn't wrong but usually you catch exceptions using a const reference,
    to avoid copying the exception object unnecessarily.

    catch (const bad_alloc&)

    > {
    > cerr << "Error allocating memory...\n";
    > return 1;
    > }
    >
    > delete p;
    >
    > return 0;
    > }
    >
    > </CODE_SNIP>
    >
    > I thank you in advance for any pointers you can provide.
    >
    > Sean


    Looks OK.

    john
    John Harrison, Jul 5, 2003
    #2
    1. Advertising

  3. Fao, Sean

    Unforgiven Guest

    Fao, Sean wrote:
    > delete p;


    In addition to what join said, this line will always be reached, even when
    an exception occurs. You may want to add a check (initialise p to NULL and
    check for that for instance, or place the delete still inside the try block)
    whether you really have any memory to delete.

    Also you are allocating with new[], do you should free with delete[]. It
    doesn't make any difference here because char is a basic type, but it's good
    practice to do it anyway: it adds clarity and gets you in the habit so you
    don't forget it when it does make a difference.

    --
    Unforgiven

    "Earth. It exists only in a corner of my memory."
    Lord Dornkirk
    The Vision of Escaflowne
    Unforgiven, Jul 5, 2003
    #3
  4. "Unforgiven" <> wrote in message
    news:be6v2j$1pggb$...
    > Fao, Sean wrote:
    > > delete p;

    >
    > In addition to what join said, this line will always be reached, even when
    > an exception occurs. You may want to add a check (initialise p to NULL and
    > check for that for instance, or place the delete still inside the try

    block)
    > whether you really have any memory to delete.


    Actually there's a return in the catch block, so the delete is not reached
    when an exception occurs.

    >
    > Also you are allocating with new[], do you should free with delete[]. It
    > doesn't make any difference here because char is a basic type, but it's

    good
    > practice to do it anyway: it adds clarity and gets you in the habit so you
    > don't forget it when it does make a difference.


    Good point.

    john
    John Harrison, Jul 5, 2003
    #4
  5. Fao, Sean

    Unforgiven Guest

    John Harrison wrote:
    > "Unforgiven" <> wrote in message
    > news:be6v2j$1pggb$...
    >> Fao, Sean wrote:
    >>> delete p;

    >>
    >> In addition to what join said, this line will always be reached,
    >> even when an exception occurs. You may want to add a check
    >> (initialise p to NULL and check for that for instance, or place the
    >> delete still inside the try block) whether you really have any
    >> memory to delete.

    >
    > Actually there's a return in the catch block, so the delete is not
    > reached when an exception occurs.


    Oops, didn't see that ^_^

    --
    Unforgiven

    "Earth. It exists only in a corner of my memory."
    Lord Dornkirk
    The Vision of Escaflowne
    Unforgiven, Jul 5, 2003
    #5
  6. Fao, Sean

    Fao, Sean Guest

    "Fao, Sean" <> wrote in message
    news:...
    > I'm reading The C++ Programming Language by Bjarne Stroustrup and I
    > was unclear as to the proper way to catch an exception thrown by the
    > new keyword. I was wondering if someobody here could let me know if I
    > have written the following code correctly.


    I appreciate your comments, I have rewritten the code using your
    recomendations.

    Thank you much...

    Sean
    Fao, Sean, Jul 6, 2003
    #6
    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. Selen
    Replies:
    0
    Views:
    2,680
    Selen
    May 28, 2004
  2. drop
    Replies:
    0
    Views:
    395
  3. Replies:
    1
    Views:
    621
    Ivan Vecerina
    May 15, 2006
  4. Replies:
    8
    Views:
    390
    James Kanze
    Mar 2, 2009
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    562
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page