55

This C++ program is a CGI script, I have no desire to deal with exceptions. I'd rather get a marginal performance boost and let the OS (Linux) handle cleanup after the process dies.

I am using the Standard C++ Library, and want any function to die like in Perl: Whenever it throws an exception. Without unwinding, or running any further code in my process.

How does -fno-exceptions work? If I have no catch at all in my code, and basically pretend like exceptions do no exist. but I do use std:: c++ library which can throw()?

19
  • 7
    @unix: Pick a lanuage: C or C++. The half-breed language invariably creates the ugliest, most broken, and hardest to maintain code.
    – GManNickG
    Aug 30, 2011 at 20:34
  • 3
    @GMan I only want to die on throw.
    – unixman83
    Aug 30, 2011 at 20:35
  • 11
    @Vlad: Extremely inefficient compared to what? And actually, no, not really. GCC has zero-overhead exceptions, which means you only "lose" speed when you throw, which is still better than checking every single error code.
    – GManNickG
    Aug 30, 2011 at 20:35
  • 15
    @Gman I highly disagree. You can pick subsets of C++ to use and you're not "having no desire to use C++" if you do that. This is what unixman is doing: picking a subset of C++ that excludes exceptions. Lots of people and companies do this. However, I do agree with you here: pick one language, C or C++. They're not nearly the same, and working in both without being more careful than most people have enough knowledge to be is likely to cause misery. Aug 30, 2011 at 20:37
  • 14
    @GMan the fact that lots of people use C++ doesn't magically make it bad, does it? Neither does lots of people picking subsets of C++ make that bad just because lots of other people who may or may not be the same people who chose a subset release bad software. Just because you don't embrace every single feature of C++ doesn't mean you shouldn't be using C++. I am not saying it's good to ignore the existence of exceptions, but I am saying that ignoring them doesn't automatically make you an infidel. Aug 30, 2011 at 20:41

5 Answers 5

91

Option #1: Simply never catch exceptions.

Exceptions don't have much overhead when they're not thrown or caught; if you're throwing and not prepared to catch, well, you're doing to die anyway, so the performance impact at that point is trivial. Note also that stack unwinding will not be performed if an exception is not handled; the program will simply terminate without performing stack unwinding.

It's important to note that, in G++, exceptions have almost no overhead when not actually thrown. G++ generates extra information sufficient to trace back the execution of the program through the stack, and some extra code to invoke destructors, etc - however none of this extra code or data is ever used until an exception is actually thrown. So you should not see a performance difference between code with exceptions enabled but not used and code with exceptions disabled (through whatever mechanism).

Option #2: Pass -fno-exceptions.

This flag instructs G++ to do two things:

  1. All exception handling in STL libraries are removed; throws are replaced with abort() calls
  2. Stack unwind data and code is removed. This saves some code space, and may make register allocation marginally easier for the compiler (but I doubt it'll have much performance impact). Notably, however, if an exception is thrown, and the library tries to unwind through -fno-exceptions code, it will abort at that point, as there is no unwind data.

This will, effectively, turn all exceptions into abort()s, as you would like. Note, however, that you will not be allowed to throw - any actual throws or catchs in your code will result in a compile-time error.

Option #3: (Nonportable and not recommended!) Hook __cxa_allocate_exception.

C++ exceptions are implemented using (among others) the __cxa_allocate_exception and __cxa_throw internal library functions. You can implement a LD_PRELOAD library that hooks these functions to abort():

void __cxa_allocate_exception() { abort(); }
void __cxa_throw() { abort(); }

WARNING: This is a horrible hack. It should work on x86 and x86-64, but I strongly recommend against this. Notably, it won't actually improve performance or save code space, as -fno-exceptions might. However, it will allow the throw syntax, while turning throws into abort()s.

6
  • 2
    +1; just don't catch exceptions. That's one of the reasons they're there for. Aug 30, 2011 at 20:36
  • 4
    "Note also that stack unwinding will not be performed if an exception is not handled; the program will simply terminate without performing stack unwinding." - this is not guaranteed by the standard. The implementation is allowed to unwind the stack (15.5.1/2). So it's OK for g++, but be careful if the code needs to be at all portable. Aug 30, 2011 at 22:16
  • 3
    @Steve, yes, all of my advice is G++-specific. After all, the standard doesn't specify any method to completely neuter exceptions :)
    – bdonlan
    Aug 30, 2011 at 22:32
  • 2
    Sure, I mentioned it just because it's the one option you offer that at first glance might look as though it's applicable to other compilers (and may be, so long as you check their docs - the behavior is implementation-defined, so it has to be documented). Aug 30, 2011 at 22:55
  • 1
    One comment on "simply not catching exceptions": The Intel compiler prevents automatic vectorizations if exceptions are enabled and may be thrown, wether they are catched or not. So it should be mentioned that completely disabling them can enable the compiler to do more optimizations.
    – Skym0sh0
    Mar 20, 2017 at 12:26
23

-fno-exceptions turns all standard library throw's into a call to std::abort(). That handles the part you can't modify directly, the rest is to not use them at all in your code.

Of course, I really doubt your justification in doing this. You only "lose" performance when you actually throw, and you're throwing out a significant and helpful bit of the language.

4
  • 1
    Wierd, my gcc says: "hi.cpp:5:8: error: exception handling disabled, use -fexceptions to enable" Aug 30, 2011 at 20:35
  • 3
    @Andre: Was that your own try-catch block? You aren't allowed to have any with exceptions disabled.
    – GManNickG
    Aug 30, 2011 at 20:36
  • Was my own throw, without catch. Aug 30, 2011 at 20:54
  • 1
    You do not 'only "lose" performance when you actually throw'. Any function which may need to be unwound may be slower. Oct 7, 2015 at 13:10
20

In case anyone stumbles upon this question, I'd like to correct what @GManNickG and (https://stackoverflow.com/a/7249460/157344) and @bdonlan (https://stackoverflow.com/a/7249442/157344) said in their answers. Unfortunately the part about "-fno-exception" removing all exception handling code and turning all throws into aborts is wrong. Well - partially wrong. This is true when you compile the library in question (libstdc++v3) with this flag, but not true if you use this library (as an .a or .so or .dll or whatever) in your own code compiled with this flag. In the latter case the exception handling code in YOUR code is forbidden, but all the calls to exception handling inside the library remain (because the library was compiled WITHOUT this flag, with exceptions enabled), so if you use new then your executable WILL have exception handling code - the only difference is that you cannot anything about these exceptions with a catch() (which is forbidden in your code), so all throws effectively end up as abort(), but only because no one catches them.

1
  • I read that this was an issue in FF. blog.mozilla.org/nnethercote/2011/01/18/… But what I don't understand is: In what kind of situation would that cause actual problems? The only situation I can imagine is where you call some STL-function with a catch clause, which calls your code, from which you call STL-code which throws. (Is there even a user-code invoking STL function with catch?) In that situation, }-destructors in your code may not be run, causing memory leaks. Am I missing a bigger problem that may occur?
    – Caesar
    Jul 29, 2021 at 1:27
11

Quote:

This C++ program is a CGI script, I have no desire to deal with exceptions.

  • Then don't. Simple. The exception will get to the top of the stack very quickly.

But I would urge you to do so. To do so means that you are thinking of the things that can go wrong.

2

Just don't catch them anywhere in your code. In that case, a termination handler will be called and your program will "crash".

1
  • 3
    -1 under-descriptive of what happens 'under-the-hood'. Just says do it a certian way, without explanation...
    – unixman83
    Aug 30, 2011 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.