This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Throwing C++ exceptions from signal handlers
- From: "Yuval Kfir" <yuvalk at mainsoft dot com>
- To: <gcc at gcc dot gnu dot org>
- Cc: "Noam Lampert" <noaml at mainsoft dot com>
- Date: Thu, 10 Jun 2004 11:50:26 +0300
- Subject: Throwing C++ exceptions from signal handlers
- Organization: Mainsoft Corporation
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