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
Just a historical note, not an answer to this question (there are plenty of good answers)... a long long time ago when dinosaurs roamed the earth and Turbo C++ was ruling the world of C++ programmers, new actually returned NULL. Quote from a book of those days (0bj3ct-0r13nt3d Pr0gr4mm1ng with ANSI and Turbo C++ - title intentionally obfuscated so that no one by mistake reads it anymore) page 115.
If the new operator fails to allocate memory it returns NULL which can be used to detect failure or success of new operator.
because of this legacy code is full with a lot of NULL checking ... a real nightmare to bring them up to the current standard...
However, here is a tiny piece of the C++ standard 5.3.4/13:
[ Note: unless an allocation function is declared with a non-throwing exception-specification (15.4), it indicates failure to allocate storage by throwing a std::bad_alloc exception (Clause 15, 18.6.2.1); it returns a non-null pointer otherwise. If the allocation function is declared with a non-throwing exception-specification, it returns null to indicate failure to allocate storage and a non-null pointer otherwise. —end note ] If the allocation function returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null.
which tells you that in some special cases, new can return NULL