C++ New Library - nothrow



Description

This is a nothrow constant and this constant value is used as an argument for operator new and operator new[] to indicate that these functions shall not throw an exception on failure, but return a null pointer instead.

Following is the declaration for std::nothrow.

		
extern const nothrow_t nothrow;

Parameters

none

Return Value

none

Exceptions

No-throw guarantee − this member function never throws exceptions.

Data races

none

Example

In below example for std::nothrow.

#include <iostream>
#include <new>

int main () {
   std::cout << "Attempting to allocate...";
   char* p = new (std::nothrow) char [1024*1024];
   if (p==0) std::cout << "Failed!\n";
   else {
      std::cout << "Succeeded!\n";
      delete[] p;
   }
   return 0;
}

The output should be like this −

Attempting to allocate...Succeeded!
new.htm
Advertisements