I’m trying to catch a divide by 0 arithmetic exception in my code but it looks like NSException is not catching that and program is terminating with message exc_arthimetic error. What is the way to catch the exception
@try {
int k;
printf("please enter k value \n");
scanf("%i",&k);
int j=1;
// testing division 1/0 condition
int i=j/k;
NSLog(@" value of i= %i",i);
}
@catch (NSException *exception) {
NSLog(@"exception is = %@ reason is %@",[exception name],[exception reason]);
}
@finally {
NSLog(@"inside finally");
}
NSLog(@" last line");
exc_arthimetic is not an exception but rather a signal sent by the OS. I would suggest checking that the denominator is not zero before trying to divide.
In order to catch this signal is more complicated and likely not worth the effort.
Take a look at
Learning Objective-C. Using Xcode 3.2.1. What is error: Program received signal: “EXC_ARITHMETIC”