| Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
| Version 1.9 Build 803 |
|
Another important portion of the exception handling mechanism is that it deletes the objects created between the point where the try/catch blocks are entered and the point where the exception is thrown. These object are automatically cleaned up when an exception occurs. The destructors are called for automatically created objects, but not for dynamically created objects which are not deleted by other objects.
The programmer should use the templated PtrHolder class to hold pointers to dynamically created objects. Because the PrtHolder object is automatically created, its destructor is called in case of an exception. That destructor deletes the pointer it holds, thus deletes the dynamically created object.
try {
// True below means it's an array, False (the default) would mean
// a singleton object.
PtrHolder<Int> iholder(new Int[10000], True);
some_function_that_throws_exception(); // pointer is deleted
} catch (...) {
cout << "Int array is nicely deleted" << endl;
}
Note that ... is standard C++ syntax and means any exception.