4 ms·
C++: RAII without exceptions
- deleted 11y ago[deleted]
- wvenable 11y agoI do some embedded development in C++11 with exceptions disabled (for size/compatibility) and using RAII to the fullest. It's all very smooth and works very well.
- corysama 11y agoBendersky explicitly put off error handling for a later post. But, that's the interesting part by far. How does your team smooth out handling errors in RAII without exceptions?
- wvenable 11y agoYou can simply return error values either as a tuple, an error code, etc. RAII, of course, takes care of the cleanup either way.
- readams 11y agoI'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.
- z92 11y agoThis is the first time I have heard "RAII requires exception". But then reading the comments section, I get that the author completely missed the point. The claimers, whoever they are, aren't claiming RAII needs exception for deallocation, which the author has covered. But they claim RAII needs exception to handle resource allocation failures. Which the author is trying to undermine saying that is a rare case. Which probably isn't that rare.
- chillaxtian 11y agothe meat of the article: "Another strong link between exceptions and RAII is in constructors. Constructors cannot have return values. Therefore, if a constructor encounters an error condition, you either throw an exception or mark some internal error state. The latter has its issues (which is why alternative methods of construction are recommended in code without exceptions), so throwing an exception is the most common approach." wish the author had provided examples of the alternative methods of construction, rather than explain RAII from scratch.
- sseagull 11y agoThe only pattern I can think of is to construct via a free function. The free function would call the real (basically empty) constructor, and then fill it in via other member functions or by directly accessing the data (via friendship). This free function would be able to allocate memory, etc, and return error flags. You could even make the real constructor private, which would prevent inadvertently creating an object with an invalid state. (Could also be done with a static member function, I suppose)
- deleted 11y ago[deleted]
- gpderetta 11y agoYes, a free or static member function that returns an Either-like object is the way to maintain strong RAII invariants while avoiding exceptions. Either, named Result, has been proposed of the next standard. Unfortunately C++ still doesn't have good facilities for dealing with monads (i.e. do notation) making working with Either (and futures, and optionals) quite awkward.