Exception Streams in C++

Here's a short but handy C++ Snippet. I was looking for a way to quickly generate runtime exceptions with useful information about the current program state. My process was:

  1. Create a string stream
  2. Build the message
  3. Throw the exception using the string from the stream

I felt like this was particularly cumbersome and quite annoying, especially for doing things like string parsing or SQL because there are a lot of places in the code where error checking is required.

In any case, I came up with this simple little class tempate which sits in a single header file and is included in the cpp file where it is to be used.



namespace utility   {

/// used to simplify the process of generating an exception message
 *  Derives from stringstream so provides an ostream interface, but throws
 *  an exception with the contents of the string when the object is destroyed
 *  \tparam Exception_t must be an exception type which accepts a
 *                      const char* in it's constructor
class ExceptionStream :
    public std::stringstream
            throw Exception_t( str().c_str() );

        std::ostream& operator()()
            return *this;

typedef ExceptionStream ex;

} /* namespace utility */

Usage is like this:

ex fExp;

fExp = evalf( m_expression["radius"].subs(m_x == iRadius));
if( is_a(fExp) )
    radius= ex_to(fExp).to_double();
    utility::ex()() << "GiNaC failed to parse radius expression: "
                    << fExp;

Here I'm using GiNaC to parse a string into a mathematical expression. If the process fails I want to throw a runtime exception (typedef'ed as utility::ex).

The class derives from string stream so it works just like a string stream… building a string by catting together the RHS of all the string operators. The magic is that the destructor for the class throws an exception. The message for the exception is the string that was built.

It's a very handy time saver… though I'm not sure if it's actually safe to use. If the destructor throws an exception, is the object memory still freed?


Comments powered by Disqus