Boost logo

Boost :

Subject: Re: [boost] [stacktrace] review (changing vote to NO)
From: Peter Dimov (lists_at_[hidden])
Date: 2016-12-25 11:16:26


Artyom Beilis wrote:

> As long as there is a simple way to throw an exception such that:
>
> 1. It is derived from std::exception
> 2. It contains information about the stack trace
>
> It is ok for me.

This is the basic way to use Boost.Exception with Stacktrace:

#include <boost/stacktrace.hpp>
#include <boost/exception/all.hpp>
#include <stdexcept>
#include <iostream>

typedef boost::error_info<struct tag_stacktrace,
boost::stacktrace::stacktrace> stacktrace_info;

template<class E> void throw_with_stacktrace( E const & e )
{
    throw boost::enable_error_info( e ) << stacktrace_info(
boost::stacktrace::stacktrace() );
}

void f()
{
    throw_with_stacktrace( std::runtime_error( "pumpkin" ) );
}

void g()
{
    f();
}

int main()
{
    try
    {
        g();
    }
    catch( boost::exception const & x )
    {
        std::cerr << boost::diagnostic_information( x ) << std::endl;
    }
}

It's also possible to build upon BOOST_THROW_EXCEPTION instead:

#include <boost/stacktrace.hpp>
#include <boost/exception/all.hpp>
#include <stdexcept>
#include <iostream>

typedef boost::error_info<struct tag_stacktrace,
boost::stacktrace::stacktrace> stacktrace_info;

#define BOOST_THROW_WITH_STACKTRACE( e ) try { BOOST_THROW_EXCEPTION( e ); }
catch( boost::exception & x ) { x << stacktrace_info(
boost::stacktrace::stacktrace() ); throw; }

void f()
{
    BOOST_THROW_WITH_STACKTRACE( std::runtime_error( "pumpkin" ) );
}

void g()
{
    f();
}

int main()
{
    try
    {
        g();
    }
    catch( boost::exception const & x )
    {
        std::cerr << boost::diagnostic_information( x ) << std::endl;
    }
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk