I’m trying to disable exceptions completely in Visual Studio 2010, and to ensure that exception handling is disabled I have the following test:
class CompilerFlagsTestExceptionConstructor
{
public:
CompilerFlagsTestExceptionConstructor() { throw std::exception("Exceptions Enabled"); }
~CompilerFlagsTestExceptionConstructor() { }
};
class CompilerFlagsTests : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE( CompilerFlagsTests );
CPPUNIT_TEST( Test_NoExceptions );
CPPUNIT_TEST_SUITE_END();
public:
void Test_NoExceptions()
{
try
{
CompilerFlagsTestExceptionConstructor object;
}
catch(std::exception)
{
assert(!"Exceptions are enabled");
}
}
};
I have checked in the property pages that exception handling is disabled, and I’ve also disabled RTTI in all my projects. I have tried setting /EH to negative as well as omitting it from the command line.
Every time I run this test, it fails and it properly throws an exception
The reason I want to disable exception handling is because the target software is written for a target architecture that doesn’t natively support exception handling and causes massive code bloat to accommodate the stack unwinding. The target architecture also has a maximum size of executable, so disabling exceptions shrinks code and also has a performance benefit. I want to disable exception handling on the windows version to ensure that the error handling to the target is working.
Am I doing something wrong? Is this test ever going to work? Is there some other way of forcing and testing that exceptions are disabled?
Thanks!
I’m not sure your test makes sense. What should the behavior be if exceptions are disabled? throw just becomes a no op? That’s what the test suggests the behavior is.
I would expect for usefully disabling exceptions throw should be a compile error, or alternatively throwing with exceptions disabled should immediately terminate the process.
But this is a digression. MSVC will emit a call to _CxxThrowException for the throw syntax. If we define our own version of this we can make it dance to our song:
This program prints out
Of course this information is provided for purely entertainment purposes and with the context that you’re doing this to match up with the error handling logic. I hope for example, that you don’t intend to ship the windows version of the program to anyone like this.