Trapping floating-point overflow in C

Viewed 1189

I am trying to trap floating-point overflow in C. Here is what I have tried

#define _GNU_SOURCE

#include <fenv.h>
#include <signal.h>
#include <stdio.h>

void catch_overflow (int sig) {   
  printf ("caught division by zero\n");   
  signal (sig, catch_overflow); 
}


int main(void) {   
  feenableexcept(FE_DIVBYZERO);   
  signal (FE_DIVBYZERO, catch_overflow);


  float a = 1., b = 0.;   float c = a/b;   return 0; }

I expected to see "caught division by zero" message, but I only get a core dump message, "Floating point exception (core dumped)". How could I modify the program to get the "caught division by zero" message?

Thanks.

1 Answers
Related