3 ms·
How do you externally detect and handle an error in a destructor? That's a rather unusual and difficult case. But, it's still a legit concern. More common an
by corysama 11y ago
How do you externally detect and handle an error in a destructor? That's a rather unusual and difficult case. But, it's still a legit concern. More common and a bit easier (but still can be awkward) is handling errors in RAII constructors.
I like RAII, but I haven't used it significantly. Meanwhile, I'm not a fan of exceptions. So, how people make the two work together smoothly is an interesting topic for me...
- blt 11y agoI recall a C++ book saying something like "ignore it, log an error, or call exit()" to handle an error in a destructor. In other words, there is no correct answer. In practice, the operations usually performed in a destructor are unlikely to fail, and in "typical" programs one of those 3 solutions is probably acceptable. But it's a serious issue in software that is supposed to be 100% robust, i.e. [1]. [1] http://250bpm.com/blog:4 http://250bpm.com/blog:4
- pandaman 11y agoThe idea with exceptions and RAII, as far as I understand, is that a RAII object, reachable through normal program flow, is always valid. This also means that we don't care about errors in destructors since an object is not reachable through normal flow at the time the destructor is executed so such an error does not have to be handled immediately and can be deferred. Without exceptions though your RAII objects can be invalid and, frankly, should be called something like RAIIUEH (Resource Allocation Is Initialization Unless Error Happened).