4 ms·
I've never heard the claim that RAII requires exceptions, and the article seems to do nothing to justify why anyone would think that. The opposite is true in t
by readams 11y ago
I've never heard the claim that RAII requires exceptions, and the article seems to do nothing to justify why anyone would think that. The opposite is true in that RAII makes handling exceptions correctly far easier than it would otherwise be.
But I'm wracking my brain trying to think why anyone would think that exceptions are needed for RAII and am unable to come up with anything.
- plorkyeran 11y agoThe very first sentence of the post links to an article where a person claims that banning exceptions means you can't use RAII.
- corysama 11y agoHow 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).
- deleted 11y ago[deleted]
- to3m 11y agoA proper RAII object has no pre-acquisition state, hence the acronym. Such objects are always valid and ready for use. That means resource acquisition has to take place in the constructor, which means you need to report errors by throwing if you're not to leave the object in some kind of non-valid state. (I'd have thought the cleanup-in-the-destructor part was more important, and thus more worthy of being emphasised in the acronym - but there you go.)
- readams 11y agoYes you can use exceptions to handle errors in the constructor, but you can just as easily do: guard x(); if (!x) { // an error occurred return x.error_code(); } x->do_stuff(); You don't have exceptions so of course you're now required to check for errors explicitly.
- cheez 11y agoThey're probably talking to C programmers. RAII is usually presented in the context of exceptions.