Chapter Contents

Previous

Next
Handling Exceptions in SAS/C

Handling Exceptions in SAS/C

Exception handling is enabled in SAS/C C++ using the except option, -Kexcept for the cross-compilers and UNIX System Services (USS). This implements exception handling as specified in the ISO C++ standard. This support includes throws, rethrows, stack unwinding, try blocks, function try blocks, and exception specifications. Code will be generated in new-expressions to perform cleanup and deallocation if an exception is thrown while constructing the new object. Also, delete expressions will attempt to destroy the remaining elements in a class array before continuing unwinding if a destructor for an element exits because of an exception. Code is also added to constructors and destructors to ensure proper cleanup if an exception is thrown while processing a sub-object.

The exception handling mechanism supports C++ and C code. An exception may be thrown in C++ code called from a C function. The C functions will be unwound, if necessary, to find a C++ handler for the exception. The exception handling mechanism uses longjmp internally. As a consequence, extra care must be taken when using exception handling in combination with C code compiled with the armode option.

It is possible to mix C++ code compiled with the except option with C++ code that was not compiled with except. This is useful to avoid supplying multiple versions of libraries or objects. However destructors for objects with automatic storage will not be invoked during exception unwinding for a C++ function compiled without the except option. Also, exception specifications will not be checked. Other exception related cleanup will also not occur in code compiled without the except option. Because of inlining and virtual functions, it can be difficult to predict the behavior of mixed code when an exception is thrown.

The C++ libraries supplied with the SAS/C C++ development system are compiled with exception handling enabled. The Rogue Wave Standard C++ library requires code that uses it to be compiled with the except option. Code that uses only the basic C++ library (run-time support and the compatibility complex and iostream classes) can be compiled without the except option. The fact that the library was compiled with the except option will be invisible unless an exception is actually thrown.

Note that the C++ libraries will throw exceptions to indicate certain conditions, as specified by the C++ standard. For example, the default operator new will throw the exception std::bad_alloc to indicate an allocation failure. Other exceptions are generated for run-time conditions involving the RTTI operators typeid and dynamic_cast<>. If there is no C++ handler to catch the exception, then the program will be terminated by a call to std::terminate.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.