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:
- Define a general exception class deriving from std::exception for the whole library.
- 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.
- Derive a new class for each specific error type.
- Avoid throwing exceptions defined for member classes: members are implementation details, so are the exceptions they throw.
- 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