Are there any some utilities that helps to show where a exception is from?

Hi, If there is an error happens, I want to throw an exception, with the error string of the following format (similar to what assert would give). main.cc:12: int main(): some message I'm wondering if boost has such facility. I'm a new boost developer, I want to understand how existing boost libraries take care of runtime errors? Thanks, Peng

On Tue, Oct 21, 2008 at 4:05 PM, Peng Yu <pengyu.ut@gmail.com> wrote:
Hi,
If there is an error happens, I want to throw an exception, with the error string of the following format (similar to what assert would give).
main.cc:12: int main(): some message
With Boost 1.37 (soon to be released), a new macro is introduced called BOOST_THROW_EXCEPTION. You use it like this: #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); } HTH, Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

Emil Dotchevski wrote:
With Boost 1.37 (soon to be released), a new macro is introduced called BOOST_THROW_EXCEPTION. You use it like this:
#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 ??? -- Eric Niebler BoostPro Computing http://www.boostpro.com

On 10/22/08, Eric Niebler <eric@boost-consulting.com> wrote:
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
???
Maybe inheriting from boost::exception instead of std::exception would solve that ? -- Alp Mestan --- http://blog.mestan.fr/ --- http://alp.developpez.com/ --- In charge of the Qt, Algorithms and Artificial Intelligence sections on Developpez

On Tue, Oct 21, 2008 at 10:18 PM, Eric Niebler <eric@boost-consulting.com> 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

At 9:58 AM -0500 10/22/08, Peng Yu wrote:
With Boost 1.37 (soon to be released), a new macro is introduced called BOOST_THROW_EXCEPTION. You use it like this:
May I ask when Boost 1.37 is scheduled to be released?
<http://www.boost.org/development/index.html> shows a target date of October 31, 2008. Since the Beta release was a few days behind their target date (21-Oct vs. 17-Oct), I suspect that we'll slip into November - but Beman (who is the release manager) knows for sure. -- -- Marshall Marshall Clow Idio Software <mailto:marshall@idio.com> It is by caffeine alone I set my mind in motion. It is by the beans of Java that thoughts acquire speed, the hands acquire shaking, the shaking becomes a warning. It is by caffeine alone I set my mind in motion.
participants (5)
-
Alp Mestan
-
Emil Dotchevski
-
Eric Niebler
-
Marshall Clow
-
Peng Yu