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:
- non-destruction of local objects (including compiler generated temporaries)
- memory leaks when destructors are not executed
- run-time errors when no catch clause is found
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.
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.
For More Information
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
#include
#include
void fx ()
{
// details omited
throw range_error(string("some info"));
}
int main ( )
{
try {
fx ();
}
catch (runtime_error& r) {
cout <
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
#include
#include
int fx ()
{
// .......
throw range_error(string("some info"));
}
int main ( )
try {
fx ();
}
catch (runtime_error& r) {
cout <
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 <
Note that the function try block ensures the exception thrown from the
member initializer is caught within the constructor.
The HP WDB Debugger and the HP/DDE Debugger support C++ exception handling.
For more information see the following:
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:
- Optimization of an automatic object with a destructor is inhibited,
since no part of the object can be in a register when a function which
may throw an exception is called. This is because run-time exception handling
uses an object's assigned address if it decides to
"clean it up" (run its destructor).
- Optimization in try blocks is inhibited because there are implicit
branches from each call in a try block to each catch clause.