C++ exceptions

I've been updating some library code to add new functionality lately. This involved some refactoring and moving classes through namespaces.

So I've been thinking about a clean way to use and manage exceptions. I like the approach of libxml++: it is clean, simple and functional. But I find it hard to emulate this approach when writing my own libraries.

These are my guidelines for code design regarding exception handling:
  1. Define a general exception class deriving from std::exception for the whole library.
  2. Derive a new exception class for each class whose methods may throw exceptions. These are declared and defined in the same header and definition files as the class throwing them.
  3. Derive a new class for each specific error type.
  4. Avoid throwing exceptions defined for member classes: members are implementation details, so are the exceptions they throw.
  5. Avoid adding extra data to exceptions such as stack info, this should be managed by throwing a different type of exceptions.

    No comments:

    Post a Comment