User's Guide
calls made through a callback mechanism). If such calls do exist, and an exception is thrown, the
unwinding can cause:
• non-destruction of local objects (including compiler generated temporaries)
• memory leaks when destructors are not executed
• runtime errors when no catch clause is found
Memory Allocation Failure and operator new
In HP aC++ programs, when either operator new ( ) or operator new [ ] cannot obtain
a block of storage, a bad_alloc exception results. This is required by the ANSI/ISO C++
International Standard.
In HP C++, memory allocation failures return a null pointer (zero) to the caller of operator new
().
To handle memory allocation failures in HP aC++ and to avoid a program abort, do one of the
following:
• Write try or catch clauses to handle the bad_alloc exception.
• Use the nothrow_t parameter to specify the type when calling operator new and check for
a null pointer.
Example:
operator new (size_t size, const nothrow_t &) throw();
operator new [] (size_t size, const nothrow_t &) throw();
.
.
.
#include <new.h>
#include <stdexcept>
class X{};
void foo1() {
X* xp1 = new(nothrow())X; // returns 0 when creating a nothrow
// object, if space is not allocated
}
void foo2() {
X* xp2 = newX: // may throw bad_alloc
}
int main() {
try {
foo1();
foo2();
}
catch (bad_alloc) {
// code to handle bad_alloc
}
catch(...) {
// code to handle all other exceptions
}
}
Possible Differences when Exiting a Signal Handler
Behavior when exiting a signal handler through a throw may differ between the two compilers.
Migration Considerations when Using Exception Handling 203