views:

1040

answers:

7

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?

A: 

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.

Adam Rosenfield
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
+5  A: 

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 ;) ..

beef2k
You 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
+2  A: 

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.

Crashworks
Ya know, enabling exceptions has absolutely no run-time hit on performance. I thought everyone knew that was a myth, now. Any arguments against exceptions are usually by old grumpy C programmers that use exceptions like return codes, which is a terrible thing to do.
GMan
Follow 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
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
It reduces code size, increasing cache performance, and removes a lot of branches, also increasing performance.
Adam Rosenfield
Don'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
IIRC, 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
A: 

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
aJ
+1  A: 

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.

Magnus Skog
A: 

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.

anon
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
I don't believe there is a legitamate reason - turning off exceptions makes about as much sense as turning off integers.
anon
Disabling C++ exceptions makes it possible to use a restricted subset of C++ in kernel mode.
bk1e
Turning 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.
anon
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
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
A: 

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.