correct way to store an exception in a variable

Posted by Evan Teran on Stack Overflow See other posts from Stack Overflow or by Evan Teran
Published on 2010-06-02T18:42:07Z Indexed on 2010/06/02 18:44 UTC
Read the original article Hit count: 255

Filed under:
|

I have an API which internally has some exceptions for error reporting. The basic structure is that it has a root exception object which inherits from std::exception, then it will throw some subclass of that.

Since catching an exception thrown in one library and catching it in another can lead to undefined behavior (at least Qt complains about it and disallows it in many contexts). I would like to wrap the library calls in functions which will return a status code, and if an exception occurred, a copy of the exception object.

What is the best way to store an exception (with it's polymorphic behavior) for later use? I believe that the c++0x futures API makes use of something like this. So what is the best approach?

The best I can think of is to have a clone() method in each exception class which will return a pointer to an exception of the same type. But that's not very generic and doesn't deal with standard exceptions at all.

Any thoughts?

© Stack Overflow or respective owner

Related posts about c++

Related posts about exception