FAQ 12.07 How can new be convinced to return NULL rather than throw an exception?
When preexception C++ code is compiled on a modern C++ compiler, the use of exceptions may cause the application to crash. In this case, there are two basic options: update the application by making it exception safe (that is, make the code do something reasonable even if something such as new throws an exception), or patch the application by replacing new Fred() with new(nothrow) Fred(). Often, but not always, it is cheaper to patch it rather than make it exception safe. The following example demonstrates this technique. #include <new> using namespace std; void sample() throw() { Fred* p = new(nothrow) Fred(); <-- 1 if (p == NULL) { <-- 2 // ... } }
|