0

Here's my method:

- (IBAction)calculateButton:(id)sender {
    NSInteger dividend = 0;
    NSInteger divisor = 0;

    @try {
        dividend = [dividendField integerValue];
        divisor = [divisorField integerValue];
        [quotientField setIntegerValue: dividend / divisor]; //program crashes here
        [remainderField setIntegerValue: dividend % divisor];
    }

    @catch (NSException *exception) {
        NSAlert* alert = [[NSAlert alloc] init];
        [alert setMessageText: @"Error"];
        [alert setInformativeText: @"Invalid input!"];
        [alert runModal];
    }

}

The way I understood it NSException is a general exception handler that all the other exceptions inherit from; so the question is, why does the program crash instead of going into the @catch block? I get an EXC_ARITHMETIC which by my understanding is still an exception... So why doesn't the @catch catch it?

Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
  • 1
    This doesn't directly answer your question, but generally exception handling is *very* rarely used in Objective C. This particular error is the archetypical example of where you should use a test instead. – sapi Sep 09 '13 at 10:26
  • but why is exception handling rarely used? if it's something that causes the program to crash, it needs to be handled... right? – Electric Coffee Sep 09 '13 at 10:29
  • 2
    Exception in Objective-C/Cocoa tend to mean programmer error, dived by zero is something you can test for. Exceptions are not very Object Orientated, exception fall through to callers methods and are not handled in an OOP way, other than having an object represent them – Nathan Day Sep 09 '13 at 10:37
  • @ElectricCoffee you test for zero before actually dividing. Since math does not define a result when you divide by zero you probably want to handle this as a special case anyway. – Alfonso Sep 09 '13 at 10:48
  • but the whole issue is that I need to check if someone puts text into the box and not numbers... division by zero isn't the problem – Electric Coffee Sep 09 '13 at 10:55
  • @ElectricCoffee Then parse the string into numbers and detect errors during the parsing process. – trojanfoe Sep 09 '13 at 10:57
  • @trojanfoe isn't that exactly what `integerValue` does? parse the string to an int I mean – Electric Coffee Sep 09 '13 at 10:58
  • @ElectricCoffee No, not exactly, as it will return `0` if the text isn't a valid number. Better is to write your own solution around `strtol()` and friends, and they *do* allow error detection. – trojanfoe Sep 09 '13 at 10:59
  • @trojanfoe that didn't make any difference – Electric Coffee Sep 09 '13 at 11:06
  • @ElectricCoffee Nevermind; this is going off-topic anyway. – trojanfoe Sep 09 '13 at 11:06
  • @ElectricCoffee see here: http://stackoverflow.com/questions/12865981/parsing-nsstring-to-double – Alfonso Sep 09 '13 at 11:20

1 Answers1

5

It's because the "exception" is not an Objective-C exception but an exception/trap from the kernel. The division by zero is not handled in objects, therefore pure C rules apply here.

Alfonso
  • 8,386
  • 1
  • 43
  • 63