C++ catch bad_alloc exception

Description

C++ catch bad_alloc exception

#include <iostream>
using namespace std;
int main()//from  ww w .  j a v  a  2 s.  c  om
{
   const unsigned long SIZE = 10000;    //memory size
   char* ptr;                           //pointer to memory
   try
   {
      ptr = new char[SIZE];             //allocate SIZE bytes
   }
   catch(bad_alloc)                     //exception handler
   {
      cout << "\nbad_alloc exception: can't allocate memory.\n";
      return(1);
   }
   delete[] ptr;                        //deallocate memory
   cout << "\nMemory use is successful.\n";
   return 0;
}



PreviousNext

Related