3 ms·
This paper suggests that people avoid C++ exceptions due to some problem with dynamic allocation or unpredictable cost or something. Maybe that's true in hard r
by quotemstr 9y ago
This paper suggests that people avoid C++ exceptions due to some problem with dynamic allocation or unpredictable cost or something. Maybe that's true in hard real-time circles, but in my day-to-day experience with C++ exception avoidance , I see very little rationality going into the decision.
I am continually frustrated by people who refuse to use C++ exceptions. These people claim to have a "philosophical objection" to the "unusable" exception model. They continually cite "bad" exception code involving try-catch clauses at every stack frame (which nobody actually writes), they claim that C++ exception handling produces "undefined" behavior (it doesn't), or that compiler support is somehow buggy (it isn't). These arguments make no sense. They have no persuasive power. They do not intersect with the real world.
Best of all, these very serious people, having written very sagacious emails banning random parts of C++ and having intoned that horrible things happen if one allows non-local flow control, switch from their email clients to their editors and go on to write Python and Java! Somehow, exceptions are cripplingly unusable in C++ yet not worth writing home about when used in Python or Java.
My theory is that most opposition to C++ exception usage is motivated mostly by cargo-culting and that most of the supposed arguments against exceptions are post-hoc rationalizations of a decision already made on the basis of non-technical factors. To wit: there were real performance and correctness problems with exceptions in early 1990s C++ compilers. These problems have been resolved for a very long time. Yet the 1990s meme that to be a responsible programmer, one should disable exceptions, or at best "use exceptions for exceptional situations" comes down to us in the present day. Too often, people blindly follow advice without seriously considering it, and it seems more likely that random bans on C++ exceptions demonstrate this phenomenon than any legitimate technical disagreement.
- dbaupp 9y ago> they claim that C++ exception handling produces "undefined" behavior (it doesn't), Exception safety (i.e. ensuring state is transactional) is hard in any language, but it's extra-hard in C++ where doing it incorrectly can lead to undefined behaviour rather than just an incorrect program. It's true that all of those sorts of problems with exceptions can occur just using normal `return`, but the hidden control flow of exceptions aggravates the problem by making it much harder to identify places of concern. > Best of all, these very serious people, having written very sagacious emails banning random parts of C++ and intoning that horrible things happen if one allows non-local flow control, switch from their email clients to their editors and go on to write Python and Java! Somehow, exceptions are cripplingly unusable in C++ yet not worth writing home about when used in Python or Java. This seems unreasonable. In general, people aren't nearly as demanding about things like performance for tasks they're solving with Python and Java as with C++. Additionally, I'm sure a significant chunk of those who dislike exceptions in C++ also dislike them elsewhere, but there's a variety of reasons that they're using any of those languages despite that (an inherited codebase, library support, ...). > To wit: there were real performance and correctness problems with exceptions in early 1990s C++ compilers. These problems have been resolved for a very long time. Clang, a C++ compiler, and LLVM, its optimizer back-end, don't use exceptions. While there are probably a variety of non-technical factors also coming into play, it seems hard to swallow that literal C++ compiler writers would entirely cargo-cult a major choice like this.
- quotemstr 9y agoThere's an element of Nissan Taleb's "Dictatorship of the Small Minority"[1] at work here. If you bring together a random group of C++ programmers, some of them will approve of exceptions and some of them will see exceptions as an abomination. The former group will grudgingly write non-exceptional code; the latter group will not write exceptional code (at least in C++). Consequently, the codebase ends up banning exceptions, the fact of which the anti-exception people use as evidence for more exception bans in other projects. This dynamic doesn't speak to the validity of the argument against exceptions. It just indicates that it's usually easiest, socially, to just appease the anti-exception people. [1] https://medium.com/incerto/the-most-intolerant-wins-the-dictatorship-of-the-small-minority-3f1f83ce4e15#.z5ry4bucq https://medium.com/incerto/the-most-intolerant-wins-the-dict...
- adrianratnapala 9y agoIn this case though, the dynamic goes both ways. Programmers brought up on modern C++ will frequently be just as intransigent against error-return values and other alternatives to exceptions. In my own experience, exceptions are useful because real programmers can't be trusted to handle errors at all. So it is better to crash the program than to allow silent corruption. And exceptions are more politically palatable than assertions. But if the quality of your team AND processes are high enough you can expect errors will actually be handled, then it is easier to do that correctly without exceptions.
- quotemstr 9y agoI hope that reasonable people in both camps can agree that blatant contract violation and assertion failure should result in fail-fast instant death. It's a sad environment that bans assertions.