The following code should print out "Received SIGFPE, Divide by Zero Exception" and then exit, but when I run it in Xcode it stops, opens the debugger, and says "Thread 1: EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)". When I run the program from the terminal, it works exactly as intended, but I'd much prefer to write my code in the Xcode IDE.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void handler_dividebyzero(int signum);
int main(int argc, const char * argv[]) {
int result = 0;
int v1 = 0, v2= 0;
void (*sigHandlerReturn) (int);
sigHandlerReturn = signal(SIGFPE, handler_dividebyzero);
if (sigHandlerReturn == SIG_ERR) {
perror("Signal Error: ");
return 1;
}
v1 = 121;
v2 = 0;
result = v1 / v2;
printf("Result of Divde by Zero is %d\n", result);
return 0;
}
void handler_dividebyzero(int signum) {
if (signum == SIGFPE) {
printf("Received SIGFPE, Divide by Zero Exception\n");
exit(0);
}
else {
printf("Received %d signal\n", signum);
return;
}
}