Unix Signals as OS Exceptions in C++
Ian Lance Taylor
ian@wasabisystems.com
Sun Sep 12 15:06:00 GMT 2004
"Steven T. Hatton" <hattons@globalsymmetry.com> writes:
> The article on the following link discusses a mechanism to handle exceptions
> generated by the Microsoft Windows OS.
>
> http://www.codeproject.org/cpp/exceptionhandler.asp#xx515459xx
>
> Since the counterpart to the Windows Structured Exception Handling is
> (probably) Unix signaling, it seems a comperable mechanism on Unix and
> Unix-like OS's would need to translate signals to exceptions. This seems to
> me to be the domain of GCC.
>
> I've never heard of such a feature. Perhaps this is just a question of RTFM.
> If so, please let me know. If not, then let me suggest the gcj may provide
> an example of how to implement this support for C++ exception handling.
>
> Is this worth pursuing?
It already works fine; just call throw from your signal handler. This
programs works as expected on i386 GNU/Linux if you type ^C during the
sleep.
#include <csignal>
#include <cstdlib>
#include <iostream>
extern "C"
void handler (int s)
{
throw s;
}
int
main ()
{
struct sigaction s;
memset (&s, 0, sizeof s);
s.sa_handler = handler;
sigemptyset (&s.sa_mask);
sigaction (SIGINT, &s, 0);
try
{
sleep (10);
}
catch (int i)
{
std::cout << "Caught signal " << i << std::endl;
exit (0);
}
std::cout << "Didn't catch anything" << std::endl;
exit (1);
}
Ian
More information about the Gcc
mailing list