We have a library that deals with many aspects of error reporting. I have been tasked to port this library to Linux. When running though my little test suite, one of the tests failed. A simplified version of the test appears below.
// Compiler: 4.1.1 20070105 RedHat 4.1.1-52
// Output: Terminate called after throwing an instance of 'int' abort
#include <iostream>
#include <csignal>
using namespace std;
void catch_signal(int signalNumber)
{
signal(SIGINT, SIG_DFL);
throw(signalNumber);
}
int test_signal()
{
signal(SIGINT, catch_signal);
try
{
raise(SIGINT);
}
catch (int &z)
{
cerr << "Caught exception: " << z << endl;
}
return 0;
}
int main()
{
try
{
test_signal();
}
catch (int &z)
{
cerr << "Caught unexpected exception: " << z << endl;
}
return 0;
}
My expectation is that the Caught exception: message will be displayed. What actually happens is that the program terminates as no catch handler appears to be present for the thrown int.
There are a few questions on SO that seem related.
I found a number of Google pages that were related.
The 'wisdom' seems to boil down to.
- Ya can't throw exceptions from signal handlers, cause the signal
handler runs with its own stack, so there are no handlers defined on it.
- Ya can throw exceptions from signal handlers, just reconstruct a fake
frame on the stack, and you are good to go.
- Ya, we do it all the time. It works for me on platform X
Ya, that used to be available with gcc, but doesn't seem to work any
more. Try the -fnon-call-exceptions option, maybe that will work
The code works as expected on our AIX/TRU64/MSVC compiler/environments. It fails in our Linux environment.
I am looking for suggestions to help resolve this issue so the library behavior on Linux will match my other platforms, or some sort or workaround that might achieve the same sort of functionality.
Letting the program core dump on signal, is not a viable option.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…