Listing 20 Implements an xalloc handler

// exhaust4.cpp
#include <stdio.h>
#include <except.h>

main()
{
   double *dp;

   try
   {
      for (int i = 0; ; ++i)
      {
         dp = new double[100];
         if ((i+1)%10 == 0)
            printf("%d allocations\n",i+1);
      }
   }
   catch(xalloc)
   {
      delete [] dp;
      puts("bad_alloc exception");
      return 1;
   }
   catch(...)
   {
      delete [] dp;
      puts("Something else happened");
      return 1;
   }
}

/* Output:
10 allocations
20 allocations
30 allocations
40 allocations
50 allocations
60 allocations
70 allocations
bad_alloc exception
*/

// End of File