|
Exception handling provides a standard mechanism for coding responses to runtime errors or
exceptions. This section is organized into the following topics:
|
| Exception Handling |
|
Exception handling provides a standard mechanism for coding responses to
runtime errors or exceptions. Exception handling is on by default. To
turn it off, you must use the +noeh option.
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 (HP aC++ runtime
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:
-
Non-destruction of local objects (including compiler generated temporaries).
-
Memory leaks when destructors are not executed.
-
Runtime errors when no catch clause is found.
|
| Exception Handling in C++ |
Following is an overview of the elements of C++ exception handling:
-
A try block encloses (logically) code that can cause an exception
that you want to catch.
-
A catch clause, which immediately follows the try block, handles an
exception of the type that can occur in the try block. The catch clause
is the exception handler. You can have multiple catch clauses associated
with a try block.
-
If an error occurs, code in the try block throws an exception to an
appropriate catch clause. The catch clause is ignored if an error does
not occur.
-
When an exception is thrown, control is transferred to the nearest
handler defined to handle that type of exception. Nearest means the
handler whose try block was most recently entered by the thread
of control, and not yet exited.
|
| Exception Handling as Defined by the ANSI/ISO C++ Intl. Std. |
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.
-
The class, exception, is the base class for object types thrown by
the Standard C++ Library components and certain expressions.
-
The runtime_error class defines errors due to events beyond the
scope of the program.
-
The logic_error class defines errors in the internal logic of
the program.
Refer to the follwing sections for more information:
|
| Basic Exception Handling Example |
The simple program shown here illustrates exception handling concepts. This program:
- contains a try block (from which a range error is thrown) and a catch
clause, which prints the operand of the throw.
- uses the runtime_error class defined in the Standard C++ Library to
report a range error.
#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 basic
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 supports C++ exception handling.
Refer to the HP WDB Debugger Documentation for more information.
|
| Performance Considerations when using Exception Handling |
|
HP aC++ exception handling has no significant performance impact at compile time or runtime.
|