17.8. Processing new Failures

When operator new fails, it throws a bad_alloc exception (defined in header <new>). In this section, we present two examples of new failing. The first uses the version of new that throws a bad_alloc exception when new fails. The second uses function set_new_handler to handle new failures. [Note: The examples in Figs. 17.517.6 allocate large amounts of dynamic memory, which could cause your computer to become sluggish.]

 1   // Fig. 17.5: fig17_05.cpp 2   // Demonstrating standard new throwing bad_alloc when memory 3   // cannot be allocated. 4   #include <iostream> 5   #include <new> // bad_alloc class is defined here  6   using namespace std; 7  8   int main() 9   {10      double ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.