Exception handling

The Taligent Application Environment uses exceptions, not error codes, to deal with unusual circumstances. Exceptions are more robust than error codes; your application should not define error codes, and functions should not signal status by returning them. Exceptions are part of C++ and are described in The C++ Programming Language (Stroustrup).

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.

Exceptions checklist

  1. Learn how exceptions work.
  2. Recover resources.
  3. Design your exceptions.
  4. Know when to throw.
  5. Know when to catch.

Exceptions syntax

To review, you throw an exception
by saying:

    throw anException;
And you catch it like this:

      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;
The type of 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).

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:

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 saying

    throw arg;
(where 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.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker