This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Unix Signals as OS Exceptions in C++


"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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]