On Tue, 19 Oct 2021 at 16:27, rajesh daggumati via Boost < boost@lists.boost.org> wrote:
rajesh daggumati
09:59 (4 hours ago) to boost-users HI, please help me to solve below issue. Shared pointers are used in my code.
Shared pointers are used in almost all code, but never for exceptions. The tool for the job is std::exception_ptr, which was first modelled in boost with boost::exception_ptr: https://en.cppreference.com/w/cpp/error/exception_ptr https://www.boost.org/doc/libs/1_77_0/libs/exception/doc/boost-exception.htm... This problem has already been solved, for all implementations, correctly. Creating your own solution is counterproductive and doomed to fail. After all, you want to be solving computational problems right? Not reimplementing what's already in the standard library. Note that you *cannot *capture the persistent address of an exception in a reliable way - even if the exception has already been transported into an exception_ptr by current_exception(). It is simply not a feature provided by the language. Once thrown, exceptions are ephemeral objects that typically occupy memory specially reserved for the purpose by the implementation. As far as you, the programmer is concerned, they only exist while your catch block is executing. Copying them at this point will lead to slicing, because you cannot enumerate all possible concrete types for a given exception base class at compile time. The only way to interrogate the captured exception is to re-throw it, for example: void interrogate(boost::exception_ptr ep) { try { if (ep) boost::rethrow_exception(ep); // if we got here there was no exception } catch(my_custom_error& e) { // it was my error } catch(std::logic_error& e) { // it was a logic error } catch(std::runtime_error& e) { // it was a runtime_error } catch(std::exception& e) { // some other standard exception } catch(...) { // something else, which we can do nothing with. } } Note that an exception_ptr is as cheap to copy as a shared_ptr. So you may as well transport it by value. In fact, under the covers it behaves the same as a shared_ptr, but with extra magic to avoid slicing or memory corruption.
class Exception : public virtual std::exception,public virtual boost::exception { --- }; class ExceptionTestException : public Exception { --- }; boost::shared_ptr<Exception> exceptionptr_t;
try { Boost_Throw_function(ExceptionTestException("Hiii")); } catch(...) { boost::exception_ptr exceptionptr = boost::current_exception(); try { boost::rethrow(exceptionptr) } catch(boost::exception &e) { //here i need to assign this 'e' to shared pointer but this e is not in heap so then how to assign this e to sharedptr(exceptionptr_t). 1 way is : boost::exception *ep = new ExceptionTestException(e); exceptionptr_t(ep); here we know that exception_ptr is having ExceptionTestException but in some cases, we dont know which type of excepiton we have in exception_ptr in runtime. then how to assign to shared_ptr
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost