r/cpp • u/User_Deprecated • Jul 11 '26
Propagating exceptions from destructors with std::exception_ptr
https://www.sandordargo.com/blog/2026/07/08/exception_ptr
80
Upvotes
r/cpp • u/User_Deprecated • Jul 11 '26
View all comments
Show parent comment
10
u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions Jul 11 '26
Current exception does utilize thread local storage but that doesn't effect your ability to pass an exception ptr around. Exception objects are allocated dynamically and an exception_ptr to that object is saved to thread local storage so current exception can fetch it when called. Since exception ptr is a ref counted smart pointer, if you make a copy, you share ownership with it. But it's not backed by thread local storage entirely. So you are free to pass this object to another thread and use it there. The thread local copy of the exception gets cleared when the code exits the exceptional path through the end of the top most catch block. (note: I say top most because you can nest try catches into catch blocks and each of those results in additonal exceptions that could exist on the same thread).
So there shouldn't be an issue with coroutines. Also I believe one of the use cases for exception_ptr was to be able to pass exceptions across threads.