I'm fine tuning a little project and I've read that I can get errors if I attempt to create a new object and it returns an error if it fails (corrupted memory, out of memory?).
So I was thinking, in the line below:
Object* myObj = new Object();
How is it usually done in projects?
Object* myObj;
try
{
myObj = new Object();
}
catch (std::bad_alloc&)
{
//Deal with the Error
}
or...
Object* myObj = new (std::nothrow) Object();
if(!myObj)
{
//Deal with the Error
}
I'm assuming #2 is the best action, since try/catch every new seems too much overhead.
Just the thought of hunting all the new keywords and adding error checking everywhere is... discouraging. Are there any other options? Or some option to make (std::nothrow) as default operation for new?