Keywords: ISO C++, library

Chapter 5. Diagnostics
Prev | Part II. Standard Contents | Next

## Chapter 5. Diagnostics

## Exceptions

### API Reference

Most exception classes are defined in one of the standard headers `<exception>`, `<stdexcept>`, `<new>`, and `<typeinfo>`. The C++ 2011 revision of the standard added more exception types in the headers `<functional>`, `<future>`, `<regex>`, and `<system_error>`. The C++ 2017 revision of the standard added more exception types in the headers `<any>`, `<filesystem>`, `<optional>`, and `<variant>`.

All exceptions thrown by the library have a base class of type `std::exception`, defined in `<exception>`. This type has no `std::string` member.

Derived from this are several classes that may have a `std::string` member. A full hierarchy can be found in the source documentation.

### Adding Data to `exception`

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
Termination | Home | Use of errno by the library