Represents a map between a signal and an exception. More...
Data Fields | |
int | signalNumber |
The signal to be converted. | |
const Exception * | exception |
The exception representing the signal. |
Represents a map between a signal and an exception.
exceptions4c can make your life easier by converting each signals to a Exception. Most of these signals would crash your program as soon as they were raised. Now you can catch
signals and avoid core dumps.
For example, you could wrap a suspicious or dangerous part of the code with try
blocks and catch
segmentation faults or divisions by zero. Then you can clean up and keep working:
int * pointer = NULL; try{ int oops = *pointer; }catch(BadPointerException){ printf("No problem ;-)"); }finally{ /* clean up... */ }
In order to perform the conversion, exceptions4c maps signals to exceptions.
The simpler way to get this working is by calling function:
extern void beginExceptionContext(bool handleSignals);
beginExceptionContext
will set up for you the default handlers for the available signals in the platform if you pass handleSignals=true
.
If you need to be more specific about which signals get converted to exceptions, you can define an array of SignalMapping
this way:
SignalMapping mapping[] = { SIGNAL_MAPPING(SIGABRT, YourVeryOwnException), /* ... */ }
...and then pass it to the function:
extern void setSignalHandlers(const SignalMapping * mapping, int mappings);
This way, only the specified signals will be handled as exceptions, and they will be converted to the specified exceptions.
These are some of the signals you can handle:
SIGFPE
when you try to divide by zero. SIGSEGV
when you make an invalid memory reference, or segmentation fault. SIGINT
when a user wishes to interrupt the process, Control-c. SIGTERM
the signal sent to a process to request its termination. The exception representing the signal.
The signal to be converted.