void set_exception_handler()
{
int err;
fenv.__control_word &= ~FE_ALL_EXCEPT;
fenv.__cs_selector &= ~FE_ALL_EXCEPT << 7;
fesetenv( &fenv );
sa.sa_sigaction = &exception_handler;
sa.sa_flags = SA_SIGINFO;
err = sigaction( SIGFPE, &sa, NULL );
if (err != 0)
printf("Cannot set FloatingPoint exception handler\n");
else
printf("[OK] SIGFPE is set\n");
}
void exception_handler(int i, siginfo_t *s, void *v )
{
if (s->si_signo == SIGFPE)
{
printf("[SIGFPE] SIGFPE Occure\n");
printf("[SIGFPE] Error number: %d\n", s->si_errno);
printf("[SIGFPE] Signal code: %d\n", s->si_code);
switch (s->si_code)
{
case FPE_INTDIV:
printf("[SIGFPE] Divison by 0\n");
longjmp( jmp , 1 );
break;
}
}
abort();
}
Compilation is easy:
gcc sigfpe.c -o sigfpe -lm
Now it will no so big problem when some error occur to properly exit or make some checks.