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/Som1Lse Jul 11 '26 edited Jul 11 '26
I don't think buffered IO is a good example. We have classes that in the standard library, namely
std::ofstream, and they solve the problem the obvious way:flushmethod which the user can use and check if they want.Then they might silently lose the data if it fails. You might not like it (I don't and I wish there was a good way to safely throw from a destructor) but there isn't currently a good alternative. (You can try messing around with
std::uncaught_exceptions, but that leaves something to be desired.)But what about the pattern mentioned in the article? Well, it doesn't solve anything here: Suppose we made a simple
<cstdio>wrapper using the pattern:What if the user forgets to check
Excafter the destructor runs? We're in the same boat where we rely on the user to check something.Edit: So, after rereading I realised it was probably meant as a counterexample to
rather than an example of using the pattern discussed in the article. It is indeed a very good counterexample to that statement.