# Chapter 19: Diagnostics

Chapter 19 deals with program diagnostics, such as exceptions and assertions. You know, all the things we wish weren't even necessary at all.

# Contents

- Adding data to exceptions
- Exception class hierarchy diagram
- Concept checkers -- new and improved!
- Verbose `terminate`

## 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
       };

Return to top of page or to the FAQ.

## Exception class hierarchy diagram

At one point we were going to make up a PDF of the exceptions hierarchy, akin to the one done for the I/O class hierarchy. Time was our enemy. Since then we've moved to Doxygen, which has the useful property of not sucking. Specifically, when the source code is changed, the diagrams are automatically brought up to date. For the old way, we had to update the diagrams separately.

There are several links to the Doxygen-generated pages from here.

Return to top of page or to the FAQ.

## Concept checkers -- new and improved!

Better taste! Less fat! Literally!

In 1999, SGI added concept checkers to their implementation of the STL: code which checked the template parameters of instantiated pieces of the STL, in order to insure that the parameters being used met the requirements of the standard. For example, the Standard requires that types passed as template parameters to `vector` be "Assignable" (which means what you think it means). The checking was done during compilation, and none of the code was executed at runtime.

Unfortunately, the size of the compiler files grew significantly as a result. The checking code itself was cumbersome. And bugs were found in it on more than one occasion.

The primary author of the checking code, Jeremy Siek, had already started work on a replcement implementation. The new code has been formally reviewed and accepted into the Boost libraries, and we are pleased to incorporate it into the GNU C++ library.

The new version imposes a much smaller space overhead on the generated object file. The checks are also cleaner and easier to read and understand.

Right now they are off by default. More will be added once GCC 3.0 is released and we have time to revisit this topic.

Return to top of page or to the FAQ.

## Verbose `terminate`

If you are having difficulty with uncaught exceptions and want a little bit of help debugging the causes of the core dumps, you can make use of a GNU extension in GCC 3.1 and later:

       #include <exception>

       int main()
       {
           std::set_terminate (__gnu_cxx::verbose_terminate_handler);
           ...
           throw anything;
       }

The `verbose_terminate_handler` function obtains the name of the current exception, attempts to demangle it, and prints it to stderr. If the exception is derived from `std::exception` then the output from `what()` will be included.

Any replacement termination function is required to kill the program without returning; this one calls abort.

For example:

       #include <exception>
       #include <stdexcept>

       struct BLARGH : std::runtime_error
       {
           BLARGH (const string& whatarg)
               : std::runtime_error(whatarg) { }
       };

       int main (int argc)
       {
           std::set_terminate (__gnu_cxx::verbose_terminate_handler);
           if (argc > 5)
               throw BLARGH("argc is greater than 5!");
           else
               throw argc;
       }

In GCC 3.1 and later, this gives

       % ./a.out
       terminate called after throwing a `int'
       Aborted
       % ./a.out f f f f f f f f f f f
       terminate called after throwing a `BLARGH'
       what(): argc is greater than 5!
       Aborted
       %

The 'Aborted' line comes from the call to abort(), of course.

Return to top of page or to the FAQ.

See license.html for copying conditions. Comments and suggestions are welcome, and may be sent to the libstdc++ mailing list.