+3  A: 

Signals are totally different than C++ exceptions. You can't use a C++ try/catch block to handle a signal. Specifically, signals are a POSIX concept, not a C++ language concept. Signals are delivered asynchronously to your application by the kernel, whereas C++ exceptions are synchronous events defined by the C++ standard.

You are quite limited in what you can do portably in a POSIX signal handler. A common strategy is to have a global flag of type sig_atomic_t which will be set to 1 in the signal handler, and then possibly longjmp to the appropriate execution path.

See here for help writing proper signal handlers.

Charles Salvia
Setting a flag is the minimum safe thing to do. That is, iff you must use a signal handler at all. Delivery of a signal is very expensive. The kernel must construct a special stack frame and push all sorts of machine-specific context onto it. Your signal handler can run safely in this frame but there are no guarantees about what exists further up the stack. That is effectively why you cannot throw... the compiler has no way to generate exception handling code for this context.
David Joyner
To some degree safety is less of a concern. The exception to be thrown is there specifically to abort the program in a meaningful fashion. There is no intent to try to restart any operations.
EvilTeach
Ok, then you're probably stuck with `longjmp` out to crash handler code. If this code is normally in a `catch` block you could factor it out to a function with `C` linkage.
David Joyner
@Charles: minor quibble that doesn't affect your point: some signals (e.g. SIGILL) are synchronous.
outis
+2  A: 

I would mask all signals in every thread, except one which would wait signals with sigwait (). This thread can handle signals without restriction, e.g. throw exceptions or use other communication mechanisms.

Bastien Léonard
This is not a multithreaded program.
EvilTeach
@EvilTeach, nevertheless I would agree with Bastien Leonard. Having a separate thread wait on `sigwait` gives you the most flexibility when it comes to dealing with signals. Otherwise, you are basically looking at using global flags and a `longjmp`, which isn't very pretty.
Charles Salvia
@Charles pop in your longjmp suggestion as an answer
EvilTeach
+1  A: 

Throwing out of a signal handler is probably not a good idea as the stack is not nesacerily set up in the same way as for function calls thus unwinding from a signal handler may not work as expected.

Important note must be taken for any register used by the C++ ABI that are saved and re-used by the signal handling mechanism.

Martin York