C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Throwing exception from inside a signal() handler

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Erwin Lotter
Guest





PostPosted: Tue Jan 18, 2005 11:01 pm    Post subject: Throwing exception from inside a signal() handler Reply with quote



Hello,

I'm wondering whether it is possible (and allowed) to throw a C++
exception from inside a signal() handler (for example to convert a
floating point exception into a C++ exception).

Thanks for your help.

Erwin Lotter

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Robert Kindred
Guest





PostPosted: Wed Jan 19, 2005 9:17 pm    Post subject: Re: Throwing exception from inside a signal() handler Reply with quote




"Erwin Lotter" <lotter_NO_S_PAM_ (AT) zsw-bw (DOT) de> wrote

Quote:
Hello,

I'm wondering whether it is possible (and allowed) to throw a C++
exception from inside a signal() handler (for example to convert a
floating point exception into a C++ exception).

I believe that Borland does this in Delphi and C++Builder, but I don't
know how they do it. I can catch exceptions, such as:

catch(bad_alloc& e) {

catch(EAccessViolation& e) {

catch(EConversionError& e) {

All of their throw items inherit ultimately from Exception, so I do not
have to use catch(...). This means that I at least have an error string
that I can report.


hth, Robert Kindred

[]
Quote:
Erwin Lotter

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Jan 19, 2005 9:18 pm    Post subject: Re: Throwing exception from inside a signal() handler Reply with quote



Erwin Lotter wrote:

Quote:
I'm wondering whether it is possible (and allowed) to throw a
C++ exception from inside a signal() handler (for example to
convert a floating point exception into a C++ exception).

According to the C standard: "If the signal occurs other than as
the result of calling the abort or raise function, the behavior
is undefined if the signal handler refers to any object with
static storage duration other than by assigning a value to an
object declared as volatile sig_atomic_t, or the signal handler
calls any function in the standard library other than the abort
function, the _Exit function, or the signal function with the
first argument equal to the signal number corresponding to the
signal that caused the invocation of the handler."

According to the C++ standard, slightly reworded, attempting to
do anything that would not be legal in a C signal handler is
implementation defined. You obviously cannot use throw in a C
signal handler, so whether you can do so in a C++ signal handler
is implementation defined.

In practice, I doubt that many implementations support the
propagation of an exception out of a signal handler. For
pratical reasons, on many implementations, this would be almost
impossible for an asynchronous signal. Given this, I suspect
that there is very little motivation to make the exception
handling mechanism "signal-safe" in general; even throwing and
catching within the signal handler is likely to cause problems
if the signal occurs during the processing of another exception.

In the end, it depends on your implementation, but unless you
can find concrete documentation guaranteeing some specific
behavior, I would assume the worst.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
matej
Guest





PostPosted: Wed Jan 19, 2005 11:21 pm    Post subject: Re: Throwing exception from inside a signal() handler Reply with quote

Signals tend to be asynchronous to your main program flow by nature. In
C++, a language, implemented on many platforms (either OS or CPU), you
cannot be sure on whether you share stack with your main program at
all. Also, signals can be raised at anytime, may be even during
unwinding some other (C++) exception... I thing it would be a disaster
to throw in signal handler...


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
dayton
Guest





PostPosted: Sat Feb 05, 2005 4:57 pm    Post subject: Re: Throwing exception from inside a signal() handler Reply with quote

Erwin Lotter wrote:
....
Quote:
I'm wondering whether it is possible (and allowed) to throw a C++
exception from inside a signal() handler (for example to convert a
floating point exception into a C++ exception).

Some compilers/run-time library combinations allow you to throw
exceptions from signal handlers. Examples of these are the HP-UX
compilers and the later Sun compilers Forte 8 and beyond?).
Compilers/RTLs that have nasty segmentation violations if you throw from
a signal handler include the GNU compilers.

Complicating the signal handling is that your application may set the
options for a signal handler to be processed on an alternate stack. In
that case it is impossible to throw an exception from the signal handler.

There is a way, though, but it requires a little assembly language
progamming on each platform you wish to support. Register your signal
handler with the sigaction() call rather than the signal() call.
Sigaction handlers take 3 parameters: the 3rd parameter usually a void
*, which usually can be cast to ucontext_t *. This is the context the
system signal handling will restore when it has finished running your
sigaction handler. Find the return address for your CPU in that
ucontext_t structure and set the return address to a C++ function that
throws your exception. That function, like a cuckoo bird, will take
over the stack frame, or nest of where the signal originally generated.

Use this technique only to handle signals that originate from your
application, such as segmentation faults, bus errors, and floating point
exceptions. If you attempt to handle the SIGINT from a control-C, for
example, the exception can be thrown from anywhere in your application,
including areas outside of your exception handling hierarchy.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.