Visual C++ has a compiler setting "Enable C++ Exceptions" which can be set to "No". What exactly will happen if I set it this way? My code never explicitly throws or catches exceptions (and therefore the first thrown exception will terminate the program anyway) and doesn't rely on stack unwinding - should I expect any more undesired behaviour from the recompiled program?
-
1I'm guess you're after a performance boost. Have you tried it? How much does disabling exceptions in your program help? – Eamon Nerbonne Dec 30 '10 at 21:18
-
Does Visual C++ support the WG21 macro: `__cpp_exceptions`? – user2023370 Jun 04 '15 at 11:49
7 Answers
The MSDN documentation of the setting explains the different exception modes and even gives code examples to show the difference between the different modes. Furthermore, this article might be interesting, even though it's pretty old.
Bottom line: The option basically enables or disables the tracking of the life-spans of all your objects. Such tracking is required, because in the case of an exception, all the proper destructors need to be called, the stack has to be unwinded, and a lot of clean up is done. Such tracking requires organizational overhead (= additional code) - this can be dropped by setting the option to "No".
I haven't tried by myself, but it looks like you still can throw
and catch
exceptions if the option is set to "No", but the clean up and unwinding is missing, which might have very bad consequences (not recommended ;) ..

- 2,233
- 2
- 19
- 20
-
3You definitely can still throw and catch - but you need to manage the lifetimes yourself. You get a pretty big advantage in terms of module size, if that is a concern to you then this can be a reason to do it – 1800 INFORMATION Jun 03 '09 at 06:13
The compiler will omit the destructors and other stack-unwinding code that cleans up after C++ objects when they go out of scope as the result of an exception.
In other words, it leaves out a bunch of cleanup code. This will significantly improve performance, but also cause grave evil if an exception actually does get thrown. (Time it yourself if you don't believe me.)
The performance difference isn't really a reason to disable exceptions, except in certain critical applications where you will be absolutely obliged to do so.

- 40,496
- 12
- 101
- 170
-
8Follow the bloody link and see for yourself. If you don't believe it, then time it yourself. I've done it on my platform. There's a cost. – Crashworks Jun 03 '09 at 05:29
-
But since I have no try-catch in my code the first thrown exception will just cause program termination. I don't see any more evil than I already have. – sharptooth Jun 03 '09 at 05:31
-
4It reduces code size, increasing cache performance, and removes a lot of branches, also increasing performance. – Adam Rosenfield Jun 03 '09 at 16:10
-
1Don't assume that throwing an exception in a program without exception support will just gracefully terminate the program. Depending on how the mechanism is implemented, if a throw occurs and the RTL doesn't include exception support, you have unpredictable behaviour. Think about doing an old-school longjmp passing a variable that was not initialized with a previous setjmp. – Fabio Ceconello Jun 03 '09 at 19:10
-
2IIRC, The main reason for the performance difference is that the compiler has to turn off some optimizations when exception handling is enabled. That is, it can't rearrange instructions so that an object is partly constructed when an exception bubbles up from a subroutine. – Die in Sente Jul 19 '09 at 23:11
When you say "NO" to "Enable C++ Exceptions" then Compiler's synchronous model of exception handling (/GX or /EHsc )is not chosen. In this mode, unwind semantics will not be enabled. That is , an object with automatic storage in the frame, between the function doing the throw and the function catching the throw, will not be destroyed.
You can refer MSDN or A Visual C++ Exception FAQ for more details on exception handling.
class Test
{
public:
Test()
{
printf("Test::constructor");
}
~Test()
{
printf("Test::Destructor");
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int a;
try
{
Test a;
int* p = 0;
*p = 0; // Cause access violation
}
catch (...)
{
printf("Caught access violation");
}
return 0;
}
with No to exception handling the output would be:
Test::constructor
Caught access violation

- 34,624
- 22
- 86
- 128
-
Is "NO" the default with MSVC then? With GCC and Clang they are on by default, and turned off with a flag (i.e. -fno-exceptions). – user2023370 Jun 04 '15 at 11:46
The code I've worked with always turns off exceptions. I haven't seen issues of corruptions or resource trashing.
I'd thought that exceptions are generally kind of a bad fit with c++. The stack blasting, especially with lack of GC makes the whole exceptions thing a pain. And then the debate about what "exceptional" really means compared with just possibilities of failures.
You will still have access to Structured Exception Handling (SEH) which can be handled by __try, __except and __finally.
C++ exception handling is just a class implementation built on top of SEH.
The compiler will also complain if you try to instantiate classes within a function that has a SEH exception handler (complains about objects needing unwind, i.e. classes), which can be a bit messy, but there are ways to get around it.

- 127,556
- 20
- 111
- 121
As far as Standard C++ is concerned, you will get undefined behaviour. The C++ Standard does not admit the possibility of exceptions being turned off globally, and certain Standard Library operations are defined as throwing exceptions under certain circumstances.
-
Okay, but what could then be a legitimate reason for disabling C++ exceptions? Looks like it's just a direct way to undefined behaviour one way or another. – sharptooth Jun 03 '09 at 08:48
-
2I don't believe there is a legitamate reason - turning off exceptions makes about as much sense as turning off integers. – Jun 03 '09 at 09:17
-
Disabling C++ exceptions makes it possible to use a restricted subset of C++ in kernel mode. – bk1e Jun 04 '09 at 07:22
-
3Turning off exceptions makes so many other C++ features near to unusable (ctors, templates, for e.g.) that you might as well use C, IMHO. At least C is a standardised language, which a "C++ subset" is not. – Jun 04 '09 at 08:13
-
As far as i know, it's implementation defined whether or not exceptions are supported in an free-standing implementation of C++. – Johannes Schaub - litb Jun 04 '09 at 21:31
-
I looked it up: It looks like they are required to work in a freestanding implementation too. Not sure where i read that they are optional. – Johannes Schaub - litb Jun 06 '09 at 19:07
If operator new
fails to allocate memory, I believe that it will return NULL
instead of throwing an std::bad_alloc
exception.
If you make any calls to third-party libraries that throw exceptions, much badness will result.

- 390,455
- 97
- 512
- 589
-
I expect that it will not cause any more badness than I have already. Since I don't catch exceptions any exception thrown anywhere will immediately cause my program termination. Did I miss anything? – sharptooth Jun 03 '09 at 04:51