Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 803
News FAQ
Search Home


next up previous contents index
Next: Creating Exception Types Up: Interface Previous: C++ Interface

Deleting Objects

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.


next up previous contents index
Next: Creating Exception Types Up: Interface Previous: C++ Interface   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2004-08-30