LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Exceptions, try...catch vs std::set_new_handler. C++ (http://www.linuxquestions.org/questions/programming-9/exceptions-try-catch-vs-std-set_new_handler-c-4175455081/)

tfnc99 03-21-2013 11:31 PM

Exceptions, try...catch vs std::set_new_handler. C++
 
So here are 2 code snippets that do essentially the same thing:
Code:

void outOfMemHandler()
{
    std::cerr << "Unable to satisfy request for memory\n";
    std::abort();
}
int main()
{
    std::set_new_handler(outOfMemHandler);
    int *pBigDataArray = new int[100000000L];
    delete [] pBigDataArray;
    return 0;
}

Code:

int main()
{
    try
    {
        int *pBigDataArray = new int[100000000L];
    }
    catch(const std::bad_alloc&)
    {
        std::cerr << "Unable to satisfy request for memory\n";
        std::abort();
    }
    delete [] pBigDataArray;
    return 0;
}

I am very confused as to which one of these is better ( -_- Its programing, there has to be some strange reason one is better than the other). Can someone explain these to me?

dwhitney67 03-22-2013 05:06 PM

Quote:

Originally Posted by tfnc99 (Post 4916342)
I am very confused ...

If you say so.

Seriously, if you want your application to "abort" at the first sign of allocation-trouble, then who cares. I'd be more worried (concerned) at the application that is so poorly developed that there is ever a concern about whether memory allocation is a concern.

In other words, if your application has exhausted all available memory, what's the next step? Allocate more memory so that you can execute the "special" command to launch a rocket towards the Moon?

eSelix 03-22-2013 05:18 PM

In that specific example, where you abort on allocation error, this without exception could be better. Exception means more code, need of enabling it, if not used elsewhere in code, etc. But if you want to do more then just quit your application, then it depends what you want to do.

With "set_new_handler" you can react on allocation errors globally. With exceptions you can react differently on every "new" error, depending on where you write "try" block. Exception also give you opurtunity to skip some code on error (for example this which use allocated memory) and do something else even in another parent function. With "set_new_handler" you can only react on insufficient memory, for example by unallocating some data and then repeat memory allocation or quit. Of course with exception you also can do that, but then need to enclose try-catch in loop. But with exception you can do everything you want if error occur.

dwhitney67 03-22-2013 05:50 PM

Quote:

Originally Posted by eSelix (Post 4916761)
... But with exception you can do everything you want if error occur.

If, and only if, your system still has memory available for use.

tfnc99 03-22-2013 09:18 PM

@dwhitney
I have always been told that there really isn't much more to do at that point (bad alloc), besides log something and print an error.

As a side question, can I count on a try..catch loop overriding whatever I have set in a handler, so I can react in special circumstances?

eSelix 03-24-2013 10:15 AM

Sorry, I do not understand your question, my english is not very good. If you ask about what happen if you use both "set_new_handler" and "try-catch" block, then first is called handler, it can quit application, repeat allocation or throw an exception. If you throw exception then later it can be caught by catch block. By default (when user handler is not set) exception is thrown.

About what to do when you have no free memory, quiting application is a last resort. Depending what you can do, you should free some memory, show message with information what happend and question what to do with user data, save or not, or if your program just executed long task (ex. brute-force cracking password), store current state to resume it later, for example after system reboot or on another machine. If error occured in some program feature you can just end executing of this one module and not whole program. For example: user invoked search feature in editor application on big text file, then it should get information about not possibility to do search and resume normal editing mode.


All times are GMT -5. The time now is 01:14 PM.