Boost logo

Boost Users :

Subject: Re: [Boost-users] [exception] nested exceptions: lost information?
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-09-23 13:27:40


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. <pascal_at_[hidden]> 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 <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 mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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