Failure of new

Discussion in 'C++' started by Girish Pal Singh, Jul 25, 2003.

  1. Hi,
    1. If the new operator fails what should be done to override it?
    2. If we want to gurantee that function does not throws exception what
    should be done?
     
    Girish Pal Singh, Jul 25, 2003
    #1
    1. Advertising

  2. Girish Pal Singh

    Rolf Magnus Guest

    Girish Pal Singh wrote:

    > Hi,
    > 1. If the new operator fails what should be done to override it?


    Catch the exception that it throws. But if you're out of memory, most
    programs can't do much more than just exit immediately, which the
    exception already does if it's not caught.

    > 2. If we want to gurantee that function does not throws exception what
    > should be done?


    If it's a function you wrote yourself, declare it as something like:

    void myfunction() throw();

    If it's an already existing function, you can't. But you can catch all
    the exceptions that it throws:

    try
    {
    somefunction();
    }
    catch(...)
    {
    std::cout << "an exception was thrown" << std::endl;
    }

    If you're specifically talking about new, you can alternatively use
    new(nothrow) from the header <new>. It will, instead of throwing an
    exception, return a null pointer in the case of failure.
     
    Rolf Magnus, Jul 25, 2003
    #2
    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. =?Utf-8?B?ZGFubWJ1ZW4=?=

    failure to create new asp.net web application

    =?Utf-8?B?ZGFubWJ1ZW4=?=, Nov 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    Patrick.O.Ige
    Nov 8, 2005
  2. Doug
    Replies:
    2
    Views:
    1,632
  3. Ilias Lazaridis
    Replies:
    0
    Views:
    605
    Ilias Lazaridis
    Feb 1, 2005
  4. Replies:
    0
    Views:
    501
  5. Replies:
    2
    Views:
    499
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page