Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off exception handling?

In the book More Effective C++ (Number 15), I read that code becomes significantly slower if exceptions are enabled, even if they are not used. In my opinion, exceptions are of limited use, and I try to avoid them, but this is another topic.

I do not entirely understand his statement:

  1. What does enabling/disabling exceptions mean? It it the difference between having zero or more than zero try/catch blocks? Is it a compiler flag? What happens if I use a DLL in which exceptions can occur?
  2. Suppose no exception is ever thrown:
    • Does the code become slower as a whole or are only the parts where the program enters/exits try/catch blocks become slower? According to the author, both is true.
  3. How can I compile without exceptions? Can I do this even if I have try/catch blocks? Can I do this if DLLs I use might throw exceptions?
like image 996
Fabian Avatar asked Feb 12 '16 11:02

Fabian


1 Answers

What does enabling/disabling exceptions mean?

Passing a flag to the compiler which disables standard conformance in relation to exceptions and makes it not generate any exception support.

What happens if I use a DLL in which exceptions can occur?

If some library handles exception internally, nothing. If it lets it escape to the caller (I never saw any library which does that, because of ABI issues, but whatever), your program crashes (in best case) as it cannot handle it. If there is a wrapper for DLL which your code include and which converts error codes to exceptions (common occurence), then it is the same as if you used exceptions in your code.

Does the code become slower as a whole or are only the parts where the program enters/exits try/catch blocks become slower? According to the author, both is true.

Notice that the book you are citing is old. Compilers are evolving. Modern compilers use zero-cost exceptions which do not incure perfomance cost if exception is not thrown. Exception handling does make executable larger as it should generate all data and code needed to process exceptions, but it should not make it slower on non-exceptional path.

How can I compile without exceptions? Can I do this even if I have try/catch blocks?

You do that in compiler-specific way. Consult your compiler documentation. Usually doing so makes compiler reject code containing any exception-related facilities, e.g. point out try as unrecognised identifier.

like image 67
Revolver_Ocelot Avatar answered Nov 08 '22 06:11

Revolver_Ocelot