conversion of catched exception to shared_ptr
rajesh daggumati
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
Op 19-10-2021 om 17:41 schreef Richard Hodges via Boost:
The only way to interrogate the captured exception is to re-throw it, for
[...] Awesome breakdown. Care to also post that on their question on SO? https://stackoverflow.com/questions/69625420/how-to-assign-catched-exception... Obviously I posted largely the same response back then, but your text is way more articulate! Seth
On Fri, 5 Nov 2021 at 18:06, Seth via Boost
Op 19-10-2021 om 17:41 schreef Richard Hodges via Boost:
The only way to interrogate the captured exception is to re-throw it, for
[...]
Awesome breakdown. Care to also post that on their question on SO?
The fact that this needs to be on SO at all tells me that the best use of the C++ standards committee would be to produce a human-readable instruction manual for the language.
https://stackoverflow.com/questions/69625420/how-to-assign-catched-exception...
Obviously I posted largely the same response back then, but your text is way more articulate!
Seth
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Richard Hodges hodges.r@gmail.com office: +44 2032 898 513 home: +376 861 195 mobile: +376 380 212
participants (4)
-
Gavin Lambert
-
rajesh daggumati
-
Richard Hodges
-
Seth