If you don't use nothrow, it throws an std::bad_alloc exception:
1 2 3 4 5 6 7 8 9 10 11
try
{
Node* listNode = new Node(value);
// guaranteed to be a good alloc if we reach here
// if it was a bad alloc, we go to our catch
returntrue;
}
catch(std::bad_alloc& exc)
{
returnfalse;
}
Note that you don't HAVE to catch the bad_alloc in this function (or at all). One of the benefits of exceptions is that they remove the necessity for error checking at every step. You can just write code and have it assume that everything has worked, and then write a single error catcher if anything went wrong at any stage.