#include #include #include void sigfpe_handler(int sig) { if (sig==SIGFPE) puts("SIGFPE handling works properly"); else puts("sigfpe_handler called, but with bad parameter"); exit(sig!=SIGFPE); } double reciprocal(double x) { return 1./x; } int main(int argc, char *argv[]) { /* signal *ought* to be enough to get SIGFPE delivered -- but it never is -- see Yorick/sysdep.c */ #include fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL); /* INV --> invalid operation DZ --> divide by zero OFL --> overflow are the three conditions you need to catch */ signal(SIGFPE, &sigfpe_handler); printf("SIGFPE handling failed, 1./0.= %g\n", reciprocal((double)(argc<-1))); exit(2); /* this is an "error" return! */ }