Lets say I have a shared library that is implemented in c++ but exposes a pure c interface. That library is then used in a c program.
Does gcc make any guarantees about what happens, if an exception escapes from the c++ library into the c application?
Will it e.g. always terminate the program?
I'm mainly interested in an answer for gcc on Linux on x64 and ARMv7-R, but answers about other operating systems, compilers and architectures are also welcome.
EDIT:
Just to make this clear: I'm not talking about letting exceptions pass through a c-function and then be caught in a calling c++ function or interaction with c or c++ callbacks. The application code itself is pure c. At some point it will call a function of the shared library (which internally is pure c++) and no application code will be called until that function returns. Also let's assume that I have no control over what flags are used to compile the application code.
Master C and Embedded C Programming- Learn as you go A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another.
The C programming language does not support exception handling nor error handling.
The try-except statement is a Microsoft extension to the C language that enables applications to gain control of a program when events that normally terminate execution occur. Such events are called exceptions, and the mechanism that deals with exceptions is called structured exception handling.
C++ try and catch: The throw keyword throws an exception when a problem is detected, which lets us create a custom error. The catch statement allows you to define a block of code to be executed if an error occurs in the try block.
From what I see about C++ exceptions, in this example which I took from MSDN, GCC seems to include the following assembly in the catch statement:
call __cxa_end_catch
jmp .L37
movq %rax, %rbx
call __cxa_end_catch
movq %rbx, %rax
movq %rax, %rdi
call _Unwind_Resume
Which makes use of what I can only assume are C++ library calls to functions that deal with exceptions (e.g. _Unwind_resume
). So if the C code links against your library it will have to provide these symbols/functions which means that the code is going to be entering the C++ library to deal with the exceptions.
However, I don't yet know what the C++ library requires in order to do its job. I would expect it to be self contained but I'm not certain of it.
Edit: The answer to this question likely lies in the answers to the following two existing questions (and their interpretation):
Edit 2: From this answer, it seems that since __cxa_throw
uses a table for keeping track of available handlers. I would assume that when the table is exhausted, which in our case occurs when we enter C code, the function would call std::terminate
. Hence, the C++ runtime (against which you must have linked) should take care of this for you without you needing to put up a catch all clause.
Since I'm still uncertain I will write up a test of this theory and update the answer with the results.
If a C++ exception escapes from the outermost try
block (which can only be within the outermost C++ function call), then the exception cannot be caught.
An uncaught exception always results in termination by std::terminate
, according to the C++ standard, §15.3/9.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With