NOTE For a good discussion of the design issues for C++ exception handling, and for reasons why exception handling is superior to error codes, see Chapter 9 of The C++ Programming Language (Stroustrup), and pages 149 through 176 of "Exception Handling for C++," in 1990 Usenix C++ Conference Proceedings.
To review, you throw an exception by saying:
Exceptions can be thrown anywhere, including inside catch clauses. A special case that is only valid inside a catch clause (or functions called from a catch clause) is:
Exceptions checklist
Exceptions syntax
And you catch it like this:throw anException;
The type of try {
...code that might throw an exception;
}
catch (const TFooException &a) {
...handle a foo exception;
}
catch (const TBarException &a) {
...handle bar exception;
}
catch (...) {
...handle anything else;
}
anException
and the argument lists to the catch
clauses can be any type; but the Taligent Application Environment convention is to throw an instance of a non-const
TStandardException subclass, and to catch it by const
reference to avoid slicing. A catch clause is found by searching up the stack until a clause is found that matches the type of the exception (using the C++ function selection rules, based on anException
's static type).
This rethrows the exception being processed with the same type as originally thrown (useful when you don't know the exact type, and you often won't). Note that sayingthrow;
(where throw arg;
arg
is the argument to your handler) does not work, because throw uses the static type. If arg
is a reference, you'll slice the object.
[Contents]
[Previous]
[Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Generated with WebMaker