please help: signals, exceptions and g++

nmeyers at javalinux.net nmeyers at javalinux.net
Sat Jan 24 07:42:45 EST 2004


On Sat, Jan 24, 2004 at 07:07:53AM -0500, Frank Ramsay wrote:
> I'm trying to write a signal handler that will convert the signal into a 
> C++ style exception, but
> I'm having a major problem, it doesn't seem to work.

I very much doubt this is possible. You want the exception to appear
be thrown by whatever method happens to be running when the signal is
received - any runtime that actually lets this happen would be considered
terribly broken.

Signals are asynchronous events that don't belong to any particular thread
of execution - even though the stack trace shows that signal-handling
is happening in the thread running main(), the runtime is correctly
shielding that implementation detail from the code and not creating the
impression that main() called sigpipeHandler() and that sigpipeHandler()
can thus throw an exception back up to main().

Nathan


> Looking at the stack it seems that the fatal error is occuring in libstc++:
> #0  0x006e3e31 in kill () from /lib/libc.so.6
> #1  0x006e3b31 in raise () from /lib/libc.so.6
> #2  0x006e4ebf in abort () from /lib/libc.so.6
> #3  0x00b80467 in __cxa_call_unexpected () from /usr/lib/libstdc++.so.5
> #4  0x00b804a4 in std::terminate() () from /usr/lib/libstdc++.so.5
> #5  0x00b80616 in __cxa_throw () from /usr/lib/libstdc++.so.5
> #6  0x080488f9 in sigpipeHandler(int) (x=13) at test.cpp:9
> #7  <signal handler called>
> #8  0x006e3e31 in kill () from /lib/libc.so.6
> #9  0x006e3b31 in raise () from /lib/libc.so.6
> #10 0x08048926 in main (argc=1, argv=0xbff67b34) at test.cpp:21
> 
> So here is my questions;
> Is it possible to do this in a safe consistant maner?
> And what am I doing wrong?
> 
> Any help would be greatly appreciated.
> 
>                                                                  -fjr
> 
> here is the code:
> 
> #include <stdio.h>
> #include <iostream>
> #include <csignal>
> 
> void sigpipeHandler(int x)
> {
>  signal(SIGPIPE, sigpipeHandler);  //reset the signal handler
>  std::cerr << "throw: " << strsignal(x) << std::endl;
>  throw strsignal(x);  //throw the exeption
> }
> 
> int
> main(int argc, char *argv[])
> {
>  int x;
> 
>  signal(SIGPIPE, sigpipeHandler);
> 
>  try
>    {
>      raise(SIGPIPE);
>    }
>  catch(const char *except)
>    {
>      std::cout << "catch: " << except << std::endl;
>    }
>  catch(...)
>    {
>      std::cout << "Exception" << std::endl;
>    }
> 
>  std::cout << "done" << std::endl;;
> 
> 
>  return 0;
> 
> }
> 
> 
> --
> Frank J. Ramsay
> fjramsay1234 at hotmail.com
> 
> _________________________________________________________________
> Find high-speed ?net deals ? comparison-shop your local providers here. 
> https://broadband.msn.com
> 
> _______________________________________________
> Discuss mailing list
> Discuss at blu.org
> http://www.blu.org/mailman/listinfo/discuss
> 
> 

-- 



More information about the Discuss mailing list