Throwing C++ exceptions from signal handlers

Yuval Kfir yuvalk@mainsoft.com
Thu Jun 10 08:51:00 GMT 2004


Hello,
is it possible to somehow convince gcc to properly throw an exception from a
signal handler context?
I saw the discussion of 2 years ago in this mailing list,
http://gcc.gnu.org/ml/gcc/2002-09/msg00034.html, and though it didn't raise
my hopes it does claim that in some cases this is being done.  How exactly?
Can it only be done for very special cases, or is there some setup that will
allow me to throw any exception from inside any signal handler?

In particular, I'd like to get the following program to run on Linux with
gcc.  On Solaris (with Sun's compiler) it works fine.
/***/
#include <stdio.h>
#include <signal.h>
#include <string.h>

void sigpipeHandler(int x)
{
    signal(SIGPIPE, sigpipeHandler);  //reset the signal handler
    fprintf(stderr,"throw: %s\n",strsignal(x));
    throw strsignal(x);  //throw the exeption
}

int main(int argc, char *argv[])
{
    int x;

    signal(SIGPIPE, sigpipeHandler);
    try
    { raise(SIGPIPE); }
    catch(const char *except)
    { fprintf(stderr,"catch: %s\n",except); }
    catch(...)
    { fprintf(stderr,"Exception\n");  }

    fprintf(stderr,"done\n");
    return 0;

}
/***/

Much obliged for any help,
- Yuval
--
Yuval Kfir, Kernel team leader
Mainsoft Corporation




More information about the Gcc mailing list