Exception Handling

Exception handling provides a standard mechanism for coding responses to run-time errors or exceptions.

Exception Handling is the Default

Exception handling is on by default. To turn it off, you must use the +noeh option.

CAUTION: If your executable throws no exceptions, object files compiled with and without the +noeh option can be mixed freely.

However, in an executable which throws exceptions (note that HP aC++ run-time libraries throw exceptions), you must be certain that no exception is thrown in your application which will unwind through a function compiled without the exception handling option turned on. In order to prevent this, the call graph for the program must never have calls from functions compiled without exception handling to functions compiled with exception handling (either direct calls or calls made through a callback mechanism). If such calls do exist, and an exception is thrown, the unwinding can cause:


Exception Handling in C++

Following is an overview of the elements of C++ exception handling:

Exception Handling as Defined by the ANSI/ISO C++ International Standard

The Standard C++ Library provides classes that C++ programs can use for reporting errors. These classes are defined in the header file <stdexcept> and described in the ANSI/ISO C++ International Standard.

For More Information


Basic Exception Handling Example

The simple program shown here illustrates exception handling concepts. This program:

#include <stdexcept> #include <iostream.h> #include <string> void fx () { // details omited throw range_error(string("some info")); } int main ( ) { try { fx (); } catch (runtime_error& r) { cout <<r.what() << '\n'; } }


Function Try Block Examples

A function can catch exceptions thrown during its execution by associating catch handlers with its body, using a function try block. Note the difference between the following example and the previous exception handling example. In this case, the try keyword comes before the function body's opening brace, and the catch handler comes after the function body's closing brace.

#include <stdexcept> #include <iostream.h> #include <string> int fx () { // ....... throw range_error(string("some info")); } int main ( ) try { fx (); } catch (runtime_error& r) { cout <<r.what() << '\n'; }

Function try blocks are sometimes necessary with class constructor destruction. A function try block is the only means of ensuring that all exceptions thrown during the construction of an object are caught within the constructor. For example:

A::A() try : _member(fx()) { cout << _member << '\n'; } catch (runtime_error& r) { cout <<r.what() << '\n'; }

Note that the function try block ensures the exception thrown from the member initializer is caught within the constructor.


Debugging Exception Handling

The HP WDB Debugger and the HP/DDE Debugger support C++ exception handling. For more information see the following:


Performance Considerations when using Exception Handling

HP aC++ exception handling has no significant performance impact at either compile-time nor run-time, if you are not using optimization. If you are using optimization, be aware of the following run-time performance implications: