r/cpp • • Apr 02 '26

What happens when a destructor throws

https://www.sandordargo.com/blog/2026/04/01/when-a-destructor-throws
102 Upvotes

48 comments sorted by

View all comments

Show parent comment

3

u/canadajones68 Apr 02 '26

The thing is, exceptions let the API consumer decide where the error checking goes. If you don't use exceptions, every possible error must be manually propagated up the chain of functions. With exceptions, you just write your happy path and it works. That's not to say exceptions are the best mechanism for all error reporting, but it is well-suited for the case where trying to ensure that an operation won't fail beforehand is a substantial portion of doing the operation. 

1

u/tyler1128 Apr 03 '26

That is both the largest benefit and largest problem with them, depending who you ask. Not having to think about the unhappy path, and the non-local control flow of it, makes robust error handling harder. It does make the logic of the expected path generally easier.

1

u/fluorihammastahna Apr 02 '26

It is quite simple to propagate errors using the type system (like a richer version of std::optional). In any case I agree: I think that for libraries good use of exceptions is often a very good way of communicating that something went wrong. Naturally, if a library consumer "disagrees" with an exception being an error for their purposes they may want to continue.