Home > Enterprise >  Can I throw exceptions through functions compiled w/o exceptions
Can I throw exceptions through functions compiled w/o exceptions

Time:12-01

Let we have two libraries: libA.a and libB.a. They are organised s.t. libA.a calls libB.a functions and provides a callback to itself. In other words, the following call stack is possible:

#0 liba_callback()
#1 libb_function()
#2 liba_function()

libA.a is compiled with -fexceptions and libB.a is compiled with -fno-exceptions.

The question is: what happens if liba_callback() throws? Can I handle this in liba_function()? Can I throw exceptions through functions compiled w/o exceptions? Is this behaviour defined?

CodePudding user response:

As @Pete Becker noted, exceptions are part of the language, so the compiler is responsible for documenting such C dialect.

The GCC documentation says:

Before detailing the library support for -fno-exceptions, first a passing note on the things lost when this flag is used: it will break exceptions trying to pass through code compiled with -fno-exceptions whether or not that code has any try or catch constructs. If you might have some code that throws, you shouldn't use -fno-exceptions. If you have some code that uses try or catch, you shouldn't use -fno-exceptions.

In particular, hitting a stack frame with no unwind information is problematic:

In particular, unwinding into a frame with no exception handling data will cause a runtime abort. If the unwinder runs out of unwind info before it finds a handler, std::terminate() is called.


To sum up:

The question is: what happens if liba_callback() throws?

abort() is called.

Can I handle this in liba_function()?

No.

Can I throw exceptions through functions compiled w/o exceptions?

No.

Is this behaviour defined?

No. It is only documented as a compiler extension.

  • Related