r/cpp • • Jul 11 '26

Propagating exceptions from destructors with std::exception_ptr

https://www.sandordargo.com/blog/2026/07/08/exception_ptr
80 Upvotes

41 comments sorted by

View all comments

Show parent comment

4

u/Som1Lse Jul 11 '26

"An error happened, and then while handling that error another error happened" is a pretty common state to need to represent.

I'm not so sure.

Take the examples you gave:

Logging, writing to disk or the network, committing to a database, etc. are all reasonable things to want to do in cleanup even though they are fallible.

If I was writing a file, and that process file, I don't care about the file being flushed. If anything I probably want it deleted. If I was doing a database transaction I'd probably want it cancelled, not committed. Same with writing to a network; I don't want a garbled packet.

I don't really see what you'd be able to do about a log failure. It seems really odd to cancel something because of a log failure, I doubt the user cares, and you can't exactly log it.

2

u/SirClueless Jul 11 '26

If I was writing a file, and that process file, I don't care about the file being flushed. If anything I probably want it deleted. If I was doing a database transaction I'd probably want it cancelled, not committed.

I would say the answer to this is clearly "It depends." If the class being destroyed represents a single transaction, then yes, cancel rather than commit. But maybe the class being destroyed represents, say, an asynchronous database connection itself, and you've already reported that other statements are accepted. In that case, the most robust way to clean up involves waiting for the ack from a server, maybe even retrying the write on another server if it is distributed.

That's why I mentioned buffered IO as an example in the first place: The tradeoff of a buffer is that you report writes as successful when they are accepted into the buffer rather than when they are flushed to the output, and in return there is no strong guarantee that the data is written to its ultimate destination. But just because there are no strong guarantees, doesn't mean the data isn't valuable and worth spending considerable effort to avoid losing.

I don't really see what you'd be able to do about a log failure. It seems really odd to cancel something because of a log failure, I doubt the user cares, and you can't exactly log it.

Why wouldn't the user care about their application being unable to log? Robust software has some kind of way of handling this: Maybe connecting to a crash reporting service, syslogging something indicating application logs are unwritable, trying to create a new log file, etc. -- doing this while unwinding the stack might not be possible, but stashing an exception somewhere and checking it before exiting or after the application catches the error and tries to log something new, would be totally reasonable.

1

u/Orlha Jul 12 '26

Uh, depends on the software I guess. Sometimes I’d rather have logs that match what actually happened. If we can’t log it (but wanted to) and we can cancel what was about to happen, then this is the way of making the logs match the facts (by cancelling the facts).