It doesn't throw an error as it is pure C — and C doesn't know how to throw errors.
if you enter the objective world of Objective-C like
NSDecimalNumber *i = [NSDecimalNumber decimalNumberWithDecimal:[@(1) decimalValue]];
NSDecimalNumber *o = [NSDecimalNumber decimalNumberWithDecimal:[@(0) decimalValue]];
NSDecimalNumber *x = [i decimalNumberByDividingBy:o];
you will see
*** Terminating app due to uncaught exception 'NSDecimalNumberDivideByZeroException', reason: 'NSDecimalNumber divide by zero exception'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff870cf50c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8abd076e objc_exception_throw + 43
2 CoreFoundation 0x00007fff870cf3bd +[NSException raise:format:] + 205
3 Foundation 0x00007fff8a476ec5 -[NSDecimalNumberHandler exceptionDuringOperation:error:leftOperand:rightOperand:] + 242
4 Foundation 0x00007fff8a475cab _checkErrorAndRound + 57
5 Foundation 0x00007fff8a47606c -[NSDecimalNumber decimalNumberByDividingBy:withBehavior:] + 159
6 devisonByZero 0x0000000100000e54 main + 484
7 libdyld.dylib 0x00007fff8aaa45c9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
But you actually can decide, what happens:
NSDecimalNumberHandler *h = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain
scale:1
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO];
NSDecimalNumber *x = [i decimalNumberByDividingBy:o withBehavior:h];
x
will be NaN
now.
But apple also provides C-types that are aware of calculation errors: NSDecimal
NSDecimal one = [@1 decimalValue];
NSDecimal zero = [@0 decimalValue];
NSDecimal result;
NSCalculationError error = NSDecimalDivide(&result, &one, &zero, NSRoundPlain);
switch (error) {
case NSCalculationNoError:
NSLog(@"result: %@", [NSDecimalNumber decimalNumberWithDecimal:result]);
break;
case NSCalculationDivideByZero:
NSLog(@"division by 0");
break;
default:
NSLog(@"some calculation error");
break;
}