rajesh
rajesh

Reputation: 69

how to assign catched exceptions to shared pointers in boost 1.56.0

try{ boost::rethrow(excep)} catch(boost::exception &e) {

} How to add this e to shared pointer.This e is not in heap area .

Upvotes: 1

Views: 133

Answers (1)

sehe
sehe

Reputation: 393537

Indeed exception pointers are special: https://www.boost.org/doc/libs/1_63_0/libs/exception/doc/exception_ptr.html (just like the corresponding [std::exception_ptr][1])

There's no probem wrapping a copy of the exception but

  • you run into object slicing issues because there's typically no virtual cloning pattern in exception hierarchies
  • it would allocate, this is usually a bad idea during exception handling (perhaps you can alleviate this with some kind of pool allocator and allocate_shared)
  • I question the use; are you looking for Transporting of Exceptions Between Threads?

That mechanism has been adopted into C++ with http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html

In short I strongly think you're looking for boost::exception_ptr or std::exception_ptr with current_exception(). [1]: https://en.cppreference.com/w/cpp/error/exception_ptr

Upvotes: 1

Related Questions