3

I often hear the C++ exception system can be disabled as you should not pay for what you do not use. If I choose to compile my C++ program without exceptions will it result in undefined behavior?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
gnillorted
  • 31
  • 2
  • 1
    Why do you believe that exceptions are costing you something if you're not using them? There is generally very little cost to be incurred unless an exception is thrown. – Cody Gray - on strike Feb 21 '11 at 00:54
  • 2
    @Cody: in general in C++ code, exceptions do cost you a tiny bit even if you don't use them, because without whole-program optimizations, each TU has to assume that other TUs might be using them. The code necessary to call destructors for automatic variables as part of stack unwinding isn't massive (and on serious compilers doesn't involve anything nearly so heavy-weight as the naive approach of laying down exception frames at runtime), but as far as I've seen there's a tiny bit more to it than if destructors only need to be called on a clean return. Compare code size. – Steve Jessop Feb 21 '11 at 01:23
  • ... normally the cost is negligible, of course, but "very little" is still "something". – Steve Jessop Feb 21 '11 at 01:25
  • @Steve: Fair enough. C++ is not my primary language, so that's good to know. But wouldn't you normally compile release versions of your software with whole-program optimizations turned on, which would decrease the cost even more? I suppose the problem is using a library for which you don't have the source, but then you're unable to turn off exceptions in the first place, so this seems like a moot point. – Cody Gray - on strike Feb 21 '11 at 02:39
  • @Cody: depends on the compiler whether there are any whole-program optimizations and what they do. I don't claim that *with* them the overhead disappears without needing to specify the option, merely that *without* them you've no chance. I guess a possibility in the case you describe is you have one TU which wraps the "dangerous" library and catch everything, and the rest of your code doesn't use exceptions at all. In principle the optimizer might deduce that the other TUs don't need exception-handling, but in practice if you care you'll specify it to be sure. – Steve Jessop Feb 21 '11 at 02:58
  • Here's a very similar question: http://stackoverflow.com/q/943087/57428 – sharptooth Feb 21 '11 at 08:06

2 Answers2

10

The current (and future) C++ standard has no notion of turning off exceptions. So technically yes, doing so leads to undefined behavior, if you ask the language lawyers. Realistically implementations try to define reasonable behavior for this popular extension. Consult your documentation.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • At least with several C++ compilers I've used (VC, GNU, GreenHills, Tasking), throwing an exception when it's disabled (because you linked exception an no-exceptions code together) will result in an ungraceful abort. Worst case is sandwiching no-exception code in the middle of a call stack of exception code, in which case none of the no-exception code will clean up, but you get a seemingly working exception. – John Ripley Feb 21 '11 at 00:23
  • The CRT will have been compiled with exceptions enabled. So you'll find you can't really turn off exceptions, unless you don't use the CRT, or at least those parts of the CRT that can throw exceptions. – Neil Feb 21 '11 at 00:27
  • 3
    Agreed, it "leads to undefined behavior" in the same way that using `long long` or threads "leads to undefined behavior". Undefined by the standard, but if your implementation documents a command-line option to do it, chances are the implementation at least attempts to define the resulting behavior. You do have to understand the consequences on your implementation. In order of difficulty, understand that runs `long long` (easiest) through to threading (hardest). – Steve Jessop Feb 21 '11 at 01:17
  • @Neil: CRT = C runtime library, which doesn't use exceptions. Much of the C++ standard library does, but these parts are by no means needed universally. – Ben Voigt Feb 21 '11 at 02:24
  • @Ben Yes, I meant the C++ runtime library. The compiler I use doesn't make the distinction between the two; C programs just link to the same library (but obviously none of the parts that use exceptions). – Neil Feb 22 '11 at 00:10
3

To add to Howard's answer.

The general issue is that code written with exceptions in mind could suddenly become bug-riddled if you turn them off.

A simple call to new for example, is supposed to throw the std::bad_alloc exception if the memory request cannot be honored. In case you turn off exceptions it'll return 0 instead: are you sure that all calls to new check that the return is not 0 ? Even those in the standard library or 3rd party libraries ?

CLang recently introduced a diagnosis that prevents compilation if throw or try are used when compiling with exceptions disabled, because it means that the code has not been properly prepared, but I am unsure wrt the other compilers, so watch out.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722