[exception] nested exceptions: lost information?
Hello
I'm trying to nest exceptions using boost.Exception, but it seems like
some information (in this case the message returned via what() ) is lost
on the way.
#include <iostream>
#include
By design, boost::copy_exception slices. Its intended use is to obtain
an exception_ptr without throwing an exception, as in:
exception_ptr p=copy_exception(foo());
(the result being that rethrow_exception(p) will throw foo.)
To copy the current exception in a catch, use
catch(...)
{
exception_ptr p=current_exception();
}
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode
On Wed, Sep 23, 2009 at 8:13 AM, Pascal Z.
Hello
I'm trying to nest exceptions using boost.Exception, but it seems like some information (in this case the message returned via what() ) is lost on the way.
#include <iostream> #include
typedef boost::error_info
nested_exception; struct file_open_error : public boost::exception, public std::exception {};
int open_file(const std::string& filename) { try { // ... throw std::runtime_error("Some internal error."); } catch(const std::exception& error) { throw file_open_error() << nested_exception(boost::copy_exception(error)); } }
int main(int argc, char* argv[]) { try { int file = open_file("foo.txt"); file++;
} catch(const boost::exception& error) { try {
boost::rethrow_exception(*boost::get_error_info
(error)); } catch(const std::exception& nested) { std::cout << "the nested error is: " << nested.what() << std::endl; } } return 0; } I except the output to be "the nested error is: Some internal error." but it outputs "the nested error is: std::exception". What can I do to make it work?
Greetings Pascal
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Emil Dotchevski schrieb:
By design, boost::copy_exception slices. Its intended use is to obtain an exception_ptr without throwing an exception, as in:
exception_ptr p=copy_exception(foo());
(the result being that rethrow_exception(p) will throw foo.)
To copy the current exception in a catch, use
catch(...) { exception_ptr p=current_exception(); }
Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode
On Wed, Sep 23, 2009 at 8:13 AM, Pascal Z.
wrote: Hello
I'm trying to nest exceptions using boost.Exception, but it seems like some information (in this case the message returned via what() ) is lost on the way.
#include <iostream> #include
typedef boost::error_info
nested_exception; struct file_open_error : public boost::exception, public std::exception {};
int open_file(const std::string& filename) { try { // ... throw std::runtime_error("Some internal error."); } catch(const std::exception& error) { throw file_open_error() << nested_exception(boost::copy_exception(error)); } }
int main(int argc, char* argv[]) { try { int file = open_file("foo.txt"); file++;
} catch(const boost::exception& error) { try {
boost::rethrow_exception(*boost::get_error_info
(error)); } catch(const std::exception& nested) { std::cout << "the nested error is: " << nested.what() << std::endl; } } return 0; } I except the output to be "the nested error is: Some internal error." but it outputs "the nested error is: std::exception". What can I do to make it work?
Greetings Pascal
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thanks, it's working now. =)
participants (2)
-
Emil Dotchevski
-
Pascal Z.