Boost logo

Boost Users :

Subject: Re: [Boost-users] [exception] array tags
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-10-25 03:06:59


On Sat, Oct 24, 2009 at 9:10 PM, Diederick C. Niehorster
<dcnieho_at_[hidden]> wrote:
> Hi,
>
> I have another question regarding the exception library. I would like
> to use the BOOST_THROW_EXCEPTION() macro, but also add some tags of my
> own at the throw site. How do I do this?
>
> How would i rewrite the below?
> typedef boost::error_info<struct tag_std_range_which,std::string>
> std_range_which;
> throw boost::enable_error_info(std::out_of_range("Matrix -=: Inputs do
> not have same shape")) <<
>            std_range_which(Rows()!=M.Rows()? "row" : "col");

I would strongly recommend that you don't add a user message as error
info. Just add the data the catch site needs to format a message. That
said...

> Simply doing
> BOOST_THROW_EXCEPTION(std::out_of_range("Matrix -=: Inputs do not have
> same shape")) <<
>            std_range_which(Rows()!=M.Rows()? "row" : "col");
> does not work

The problem is that std::out_of_range does not derive from
boost::exception. This is the precise situation
boost::enable_error_info is designed for:

BOOST_THROW_EXCEPTION(boost::enable_error_info(std::out_of_range("Matrix
-=: Inputs do not have same shape")) <<
std_range_which(Rows()!=M.Rows()? "row" : "col");

Or (if you throw std::out_of_range a lot) just derive from it and
boost::exception, then you don't need to use enable_error_info:

struct my_out_of_range: virtual std::out_of_range, virtual boost::exception { };

BOOST_THROW_EXCEPTION(my_out_of_range() <<
std_range_which(Rows()!=M.Rows()? "row" : "col");

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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net