Keywords: ISO C++ , library

Adding Data to Exceptions
Prev | Chapter 7. Exceptions | Next

## Adding Data to Exceptions

The standard exception classes carry with them a single string as data (usually describing what went wrong or where the 'throw' took place). It's good to remember that you can add your own data to these exceptions when extending the hierarchy:

       struct My_Exception : public std::runtime_error
       {
         public:
           My_Exception (const string& whatarg)
               : std::runtime_error(whatarg), e(errno), id(GetDataBaseID()) { }
           int  errno_at_time_of_throw() const { return e; }
           DBID id_of_thing_that_threw() const { return id; }
         protected:
           int    e;
           DBID   id;     // some user-defined type
       };

Prev | Up | Next
Chapter 7. Exceptions | Home | Chapter 8. Concept Checking