new, No Memory Allocation

via Dynamic memory – C++ Tutorials.

When new throws exception

#include<iostream>
#include<new>

using namespace std;

int main(void)
{
     try
     {
          int * ptr = new int[100000000];
     }
     catch(bad_alloc &err)
     {
          cerr<<"Error: "<<err.what();
     }
}

When memory cannot be allocated further, then returning NULL from new expression

...
int *ptr = new (nothrow) int;
if(ptr == NULL) {cerr<<"Memory cannot be allocated.";}

If in the first case no try-catch would be used then program will crash. The first one is better than the second, because it is not required to check explicitly for the ptr to be NULL.