Hi, I have just discovered boost::exception and am very happy I did. One thing i would like to do, but haven't yet figure out: I have a matrix class for which I want to throw exceptions when out_of_range requests are made. One of the extra tags I want to attach is the dimensions of the matrix and it would be neat if i can put those in an array. Defining the tag is no problem, getting the info in there is: defining: typedef boost::error_info<struct tag_std_range_size,size_t[2]> std_range_size; I have tried: << std_range_size({m_nRows, m_nCols}); size_t a[2] = {m_nRows, m_nCols}; << std_range_size(a); and desperately even << std_range_size() = {m_nRows, m_nCols}; which doesn't make much sense Can I do such a thing, should I use something else than a plain array or do i have to resort to two separate tags for nRows adn nCols respectively? Thank you for your help and advice! Best, Diederick
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"); 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, nor does BOOST_THROW_EXCEPTION(std::out_of_range("Matrix -=: Inputs do not have same shape") << std_range_which(Rows()!=M.Rows()? "row" : "col")); Thank you! Diederick
Diederick C. Niehorster wrote:
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, nor does BOOST_THROW_EXCEPTION(std::out_of_range("Matrix -=: Inputs do not have same shape") << std_range_which(Rows()!=M.Rows()? "row" : "col")) Maybe because std::out_of_range is not a boost::exception and dosn't support << ?
___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
Hi Joel, Thanks for your reply! That is indeed the problem, I am wondering if there is a way to get around it so i can still use BOOST_THROW_EXCEPTION as it gives me the convenience of automatically appending function, file and line information. BOOST_THROW_EXCEPTION is a macro that is defined as follows (http://www.boost.org/doc/libs/1_40_0/libs/exception/doc/BOOST_THROW_EXCEPTIO...): #define BOOST_THROW_EXCEPTION(x)\ ::boost::throw_exception( ::boost::enable_error_info(x) <<\ ::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\ ::boost::throw_file(__FILE__) <<\ ::boost::throw_line((int)__LINE__) ) Rethinking the problem from the perspective of your post, I did get something that compiles, but adds some syntax: 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")); Not ideal, it would be nice if i don't have to add the boost::enable_error_info() call manually, but I can work with it! Thanks, Dee On Sun, Oct 25, 2009 at 2:39 PM, joel <joel.falcou@lri.fr> wrote:
Diederick C. Niehorster wrote:
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, nor does BOOST_THROW_EXCEPTION(std::out_of_range("Matrix -=: Inputs do not have same shape") << std_range_which(Rows()!=M.Rows()? "row" : "col"))
Maybe because std::out_of_range is not a boost::exception and dosn't support << ?
___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Diederick C. Niehorster wrote:
Not ideal, it would be nice if i don't have to add the boost::enable_error_info() call manually, but I can work with it!
What I did is defining my own proper hierarchy of exception so i can pass them as I see fit without problem. -- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
On Sat, Oct 24, 2009 at 9:10 PM, Diederick C. Niehorster <dcnieho@gmail.com> 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
Hi Emil and Joel, Thank you for your valuable suggestions. As you might have noticed I'm rather new to exceptions (in general and the boost library), this helped a lot. Thanks! Diederick On Sun, Oct 25, 2009 at 3:06 PM, Emil Dotchevski <emildotchevski@gmail.com> wrote:
On Sat, Oct 24, 2009 at 9:10 PM, Diederick C. Niehorster <dcnieho@gmail.com> 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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Sun, Oct 25, 2009 at 12:45 AM, joel <joel.falcou@lri.fr> wrote:
Emil Dotchevski wrote:
struct my_out_of_range: virtual std::out_of_range, virtual boost::exception { };
Why virtual inheritance ?
See http://www.boost.org/doc/libs/release/libs/exception/doc/using_virtual_inher.... Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
Hi Emil, On Sun, Oct 25, 2009 at 3:06 PM, Emil Dotchevski <emildotchevski@gmail.com> wrote:
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");
This does not compile (on VS2008), giving me at call site: error C2512: 'my_out_of_range::my_out_of_range' : no appropriate default constructor available I have tried adding a default constructor, struct my_out_of_range: virtual std::out_of_range, virtual boost::exception {my_out_of_range(){};}; but then i get the error: error C2512: 'std::out_of_range::out_of_range' : no appropriate default constructor available I must be missing something painfully obvious here, but can't figure it out. Do you know what is going wrong? Best and thanks, Diederick
On Sun, Oct 25, 2009 at 12:46 AM, Diederick C. Niehorster <dcnieho@gmail.com> wrote:
On Sun, Oct 25, 2009 at 3:06 PM, Emil Dotchevski <emildotchevski@gmail.com> wrote:
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");
This does not compile (on VS2008), giving me at call site: error C2512: 'my_out_of_range::my_out_of_range' : no appropriate default constructor available I have tried adding a default constructor, struct my_out_of_range: virtual std::out_of_range, virtual boost::exception {my_out_of_range(){};}; but then i get the error: error C2512: 'std::out_of_range::out_of_range' : no appropriate default constructor available
Right, I forgot that std::out_of_range takes a string. You correctly wrote a default constructor for your exception type, in that constructor simply pass a string literal to std::out_of_range's constructor: my_out_of_range::my_out_of_range(): std::out_of_range("My error") { } (That string will show up from the what() member function of std::exception.) Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Sat, Oct 24, 2009 at 8:09 PM, Diederick C. Niehorster <dcnieho@gmail.com> wrote:
Hi,
I have just discovered boost::exception and am very happy I did. One thing i would like to do, but haven't yet figure out:
I have a matrix class for which I want to throw exceptions when out_of_range requests are made. One of the extra tags I want to attach is the dimensions of the matrix and it would be neat if i can put those in an array. Defining the tag is no problem, getting the info in there is:
defining: typedef boost::error_info<struct tag_std_range_size,size_t[2]> std_range_size;
I have tried: << std_range_size({m_nRows, m_nCols}); size_t a[2] = {m_nRows, m_nCols}; << std_range_size(a); and desperately even << std_range_size() = {m_nRows, m_nCols}; which doesn't make much sense
Option 1, you can use boost::tuple with two separate tags: #include <boost/exception/info_tuple.hpp> typedef boost::error_info<struct std_range_rows_,size_t> std_range_rows; typedef boost::error_info<struct std_range_cols_,size_t> std_range_cols; typedef boost::tuple<std_range_rows,std_range_cols> std_range_size; With this in place you can throw whatever() << std_range_size(m_nRows,m_nCols); The above still adds two individual error infos: at the catch site you'd still need separate get_error_info calls to get the rows and the cols. Option 2, you can put rows and cols in a struct and make that your error_info type: struct rc { size_t rows, cols; rc( size_t r, size_t c ): rows(r), cols(c) { } }; typedef boost::error_info<struct std_range_size_,rc> std_range_size; and then throw whatever() << std_range_size(m_nRows,m_nCols); Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
participants (3)
-
Diederick C. Niehorster
-
Emil Dotchevski
-
joel