Exceptions can't be used in the place of signals. Signals are a form of interprocess communication where exceptions are not.
There are several different signals that can be caught. In this example the interrupt signal (control C) is caught.
The ignore is put in the handler so we don't get a signal while we are processing the signal. The rule of thumb is to keep the code in the handler as short as possible.
Code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
static void
sig_int()
{
/* Signal Interrupt, Captures Control-C */
if ((int)signal(SIGINT, SIG_IGN) == -1) /* Ignore ^C */
perror("Signal Error");
printf( "Control-C is caught\n\n>" );
if ((int)signal(SIGINT, sig_int) == -1)
perror("Signal Error");
return;
}
main()
{
char strIn[256];
// if ((int)signal(SIGINT, SIG_IGN) == -1) /* Ignore ^C */
// perror("Signal Error");
if ((int)signal(SIGINT, sig_int) == -1) /* catch ^C */
perror("Signal Error");
while( 1 )
{
printf( "> " );
gets( strIn );
printf( "%s\n", strIn );
}
}