I'm working with a C/Fortran library from C++ and the library calls exit(). I would like it to throw an exception so that the destructors in my C++ program will be called. I have been able to create my one definition of exit that throws an exception, but then terminate is still called. Is there to prevent terminate from being called and allow the normal exception handling to happen?
UPDATE: In the comments it was pointed out that this works on x64 but fails on x86, so the main question is "is there is a way to make x86 work like x64?".
UPDATE2: See my response about why this wasn't working on x86 and how to fix it.
Here's my test code:
test_exception.c
#include <stdlib.h>
void call_c() { exit(1); }
test_exception.cpp
#include <iostream>
#include <stdexcept>
extern "C" void call_c();
extern "C" void exit(int value)
{
throw std::runtime_error(std::string("Throwing an exception: ") + char('0' + value));
}
int main()
{
try {
call_c();
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
Built with the following commands:
gcc -c test_exception.c -o test_exception_c.o
g++ -c test_exception.cpp -o test_exception_cpp.o
g++ test_exception_c.o test_exception_cpp.o -o test_exception