Custom C++ exception class with stack trace generation I wanted to implement my own runtime error class in C++, that could provide more meaningful information on where the error occurred than std::runtime_error does.

I decided to use boost::stacktrace to generate a stack trace, appended to the exception message.

Anyway, this is my code:

/**
    \brief Represents a runtime error.

    Equivalent of std::runtime_error, but contains a stacktrace
    generated by Boost.
*/
class Exception : public std::runtime_error
{
    public:
        /**
            \brief Returns error description.

            If the backtrace was generated successfully,
            returns the message along with stack trace.
            Otherwise, behaves like std::runtime_error::what().
        */
        const char *what( ) const noexcept override
        {
            if ( has_backtrace )
                return message.c_str( );
            else
                return std::runtime_error::what( );
        }

        /**
            \brief Constructs an exception, generates a backtrace if possible.

            \param cmessage The error description.
        */
        Exception( const char *cmessage ) :
            std::runtime_error( cmessage ) // std::runtime_error will store the original message
        {
            // Attempt to generate stacktrace
            try
            {
                std::stringstream buffer;
                buffer &lt;&lt; cmessage &lt;&lt; std::endl &lt;&lt; boost::stacktrace::stacktrace( );
                message = buffer.str( );
                has_backtrace = true;
            }
            catch ( std::exception &amp;ex )
            {
                has_backtrace = false;
            }
        }

    protected:
        bool has_backtrace; //!&lt; Determines if backtrace is present
        std::string message; //!&lt; Contains the user message along with backtrace.
};


I can tell it works on this simple example

void foo( )
{
    throw Exception( "some message" );
}

void bar( )
{
    foo( );
}

int main( )
{
    try
    {
        bar( );
    }
    catch ( std::exception &amp;ex )
    {
        std::cerr &lt;&lt; ex.what( );
    }

    return 0;
}


because it generates following message:

some message
 0# Exception::Exception(char const*) in ./ex
 1# foo() in ./ex
 2# bar() in ./ex
 3# main in ./ex
 4# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
 5# _start in ./ex


However, what I'd like to know is if my code isn't too complicated
as for an exception class and if it properly handles possible stack trace
generator exceptions and properly falls back to std::runtime_error functionality. 

Also, is there some important property/method that all exception classes should have, that I had overlooked and forgot to implement? Maybe the whole idea of having such class is bad in general and I should use some other solution?

Other suggestions are obviously welcome too.
Exception( const char *cmessage ) :


I strongly recommend making this constructor explicit, so that you don't permit the user to accidentally write

void some_function(const Exception&amp;);
...
some_function("hello world");


My impression is that most C++ experts recommend explicit on one-argument constructors; I personally hold the minority opinion that explicit should be present on all constructors by default (i.e., if you don't have a specific immediate need for implicit conversion, you should just put explicit automatically, and sort out the consequences later if necessary).



catch ( std::exception &amp;ex )


My current employer's codebase actually does catch by non-const reference, but if you're starting from scratch, I strongly recommend catching by const reference by default. Accidentally mutating the current exception object can cause race conditions.



Also, in the Exception constructor, your catch (std::exception&amp;) should almost certainly be catch (...), right?



As for the high-level point of what you're doing, I believe I have an outline of this sort of thing under "Mix-Ins" somewhere near the end of Template Normal Programming. (Sorry, I'm rushed right now, but I can come back and give you a real link later.) (LATER: I elided that part of the talk, but the two slides on mixins are visible very briefly here.)

The first improvement is to make your thing a template inheriting from T instead of from std::runtime_error specifically. Second, I'd make a function my::throw_with_stacktrace(T&amp;&amp; ex) that simply does throw Exception&lt;std::remove_reference_t&lt;T&gt;&gt;(std::forward&lt;T&gt;(ex));.

Gotta run, but I hope this helps a bit.