What compiler flags do I use to completely get rid of the exception handling? I don't use them at all but the compiler still generates .pdata and .xdata sections which there for exception handling only.
-
IIRC They are disabled by default. – Dec 22 '17 at 19:48
-
@RSahu i did. There is no such a flag. – 123qwe Dec 22 '17 at 19:49
-
2I don't use at all the NumLock key, but the PC makers still put it on my keyboard. How can I get rid of it ? – Christophe Dec 22 '17 at 19:57
-
@Christophe i'm 12 so i would say make your uwn standart. May be people should not use this key, really and you are the one who can explain it well enough so that the standart change. I don't use exceptions because they shoulnot even exist, just like multiple inheritance, rtti and so on. I have a plan to change the industy in the near future. – 123qwe Dec 22 '17 at 20:03
-
@123qwe joke aside, unless you should think of potential consequences of mixing different exception handling code generation (e.g. your code and the code of your libraries), as explained here: https://stackoverflow.com/questions/6524259/what-are-the-consequences-of-mixing-exception-handling-models-in-visual-studio-2 – Christophe Dec 22 '17 at 20:12
2 Answers
In your project properties set the Enable C++ Exceptions to No:
and enable the unwind semantics by specifying the /EHsc
flag:
To set this option on command line you would leave out the /EHa
, /EHsc
and /EHs
flags from the [option]
section as described in the Compiler Command-Line Syntax manual.
Further relevant MSDN reading:

- 14,674
- 4
- 34
- 47
-
3@ what is the option to turn off exceptions in the command line and not in the VS? – 123qwe Dec 22 '17 at 20:04
-
-
I downvoted because I think by now we have a more solid procedure and Knowledge Base, to do this. As I described in my answer below. In 2017 Ron could not predict the future, mu downvote is not a personal attack. I think anonymous downvoting should be stopped. – Chef Gladiator Dec 30 '20 at 21:38
-
Setting No will remove /EHsc, which stands for Yes in "Enable C++ Exceptions" automatically, and readding same thing manually does not seem to have any observable effect – Володимир Ніколайчук Aug 12 '22 at 21:05
This is my experience. And practice.
To disable C++ exceptions using cl.exe, one should do all of the following:
- /EHsc or any other /EH combination, should not exist as a switch,
- /D_HAS_EXCEPTIONS=0 must be used
- /GR- disables the RTTI
That combination makes try/throw/catch
into non-existent keywords. That is: you are in the non-standard C++ territory.
The cl.exe
compiler always generates code that supports asynchronous structured exception handling (SEH)
That means C or C++, SEH is "always there". I personally do use it. MS STL compiles and it is quite fast with the above switches combination. In (almost all of my) main()
I catch the potential SE raised and then I create a minidump which Visual Studio can open and lead you to the exact position in code where the exception was raised.
We need to talk about the /kernel
switch. It switches C++ exceptions off, in one go, and instructs the linker to produce "kernel mode" win executables, drivers, and such. /GR-
still has to be used manually. I used it for standard desktop/console apps with no problems. Always using SEH of course.
NOTE: _CPPUNWIND
you can not set. But you do use it in your code to check if C++ exceptions are on or off.
HTH
PS: I always use static runtime lib's: switches /MT
and /MTd
. I think these days one does not need to do anything else in case dll runtimes are used, switches: /MD
and /MDd
. I am not 100% sure, please check e.g. here.

- 902
- 11
- 23
-
1
-
Thanks, @strager :) perhaps `/EHs-c-` is not necessary. But as long as `_CPPUNWIND` behaves as expected it does not matter :) – Chef Gladiator Sep 10 '21 at 12:54