Boost logo

Boost :

Subject: Re: [boost] Are there any some utilities that helps to show where a exception is from?
From: Emil Dotchevski (emil_at_[hidden])
Date: 2008-10-22 13:42:17


On Tue, Oct 21, 2008 at 10:18 PM, Eric Niebler
<eric_at_[hidden]> wrote:
> Emil Dotchevski wrote:
>> #include "boost/throw_exception.hpp"
>>
>> struct my_exception: std::exception { };
>>
>> BOOST_THROW_EXCEPTION(my_exception());
>>
>> It automatically records the throw location in the exception object.
>> At the catch site, you can use boost::diagnostic_information to get a
>> string that includes the throw location:
>>
>> #include "boost/exception/diagnostic_information.hpp"
>>
>> catch( my_exception & e )
>> {
>> std::cerr << boost::diagnostic_information(e);
>> }
>
> When I try this (on trunk) I get:
>
> 1>.\main.cpp(93) : error C2664: 'boost::diagnostic_information' : cannot
> convert parameter 1 from 'my_exception' to 'const boost::exception &'
> 1> Reason: cannot convert from 'my_exception' to 'const
> boost::exception'
> 1> No user-defined-conversion operator available that can perform
> this conversion, or the operator cannot be called

Ah right, my mistake. As Alp suggested deriving from boost::exception
would work, though you should always derive from std::exception as
well, since this is a requirement of boost::throw_exception (which
BOOST_THROW_EXCEPTION calls after recording throw location information
in the exception.)

The other possibility is to not derive from boost::exception but
dynamic_cast to it once you catch my_exception:

catch( my_exception & e )
{
  if( boost::exception * be=dynamic_cast<boost::exception *>(&e) )
    std::cerr << boost::diagnostic_information(*be);
  else
    std::cerr << e.what();
}

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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