Why doesn\'t new return NULL on failure? Why does it throw an exception only on failure?
As it returns a pointer to the object on successes
It is by design. In C++ every kind of failure is notified by throwing an exception by default — streams, however, are exception, which does not throw exception (pun intended) by default.
You can use nothrow version as:
T *p = new (std::nothrow) T(args);
if ( p == nullptr ) //must check for nullity
{
std::cout << "memory allocation failed" << std::endl;
}