|
Boost Users : |
Subject: [Boost-users] [exception] nested exceptions: lost information?
From: Pascal Z. (pascal_at_[hidden])
Date: 2009-09-23 11:13:06
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 <boost/exception.hpp>
typedef boost::error_info<struct tag_nested_exception,
boost::exception_ptr> 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<nested_exception>(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 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