A calculator class seems to be a popular starting point for those learning Objective-C. So in my 'calculator' class, I have defined my addition function to use the 'decimalNumberByAdding:withBehavior:' method. The '_behavior' is defined in the init method. The code described is shown below:
-(id)init
{
if ((self = [super init]))
{
// Yes, raise errors (or exceptions)
// on all of these cases,
// and control the scale (number precision).
_behavior = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundPlain
scale:NSDecimalNoScale
raiseOnExactness:YES
raiseOnOverflow:YES
raiseOnUnderflow:YES
raiseOnDivideByZero:YES];
}
return self;
}
// My addition method
-(NSDecimalNumber *)addNum1:(NSDecimalNumber *)num1
andNum2:(NSDecimalNumber *)num2
{
NSDecimalNumber *result = [num1 decimalNumberByAdding:num2
withBehavior:_behavior];
return result;
}
To properly use the NSDecimalNumberBehaviors protocol requires implementing 3 methods: '- roundingMode', '- scale', and '- exceptionDuringOperation:error:leftOperand:rightOperand:' (However, isn't roundingMode and scale already specified in my NSDecimalNumberHandler above, called '_behavior'?).
The motivation for using this protocol is to raise and handle an exception if one of the errors occur as defined in the '_behavior' (such as an overflow or loss of precision error).
After searching the web and Apple's documentation, it is not yet clear how to use the NSDecimalNumberBehaviors protocol, and properly raise and handle the errors that are described in the '_behavior' above (the protocol implementation seems to include redundancies when compared with the '_behavior' declaration).
What is the proper way to use the NSDecimalNumberBehaviors protocol, and raise and handle the errors that are defined in the '_behavior'? Perhaps an example would help most. Thanks : )
--Update:
Upon looking again at the documentation, I now see that the NSDecimalNumberHandler adopts the NSDecimalNumberBehaviors protocol. So that means I just need to initialize and use the NSDecimalNumberHandler properly.
After having added a division method (in order to test the divide by 0 exception), I see that the code can look more like this:
-(id)init
{
if ((self = [super init]))
{
// Yes, raise errors (or exceptions)
// on all of these cases,
// and control the scale (number precision).
_behavior = [NSDecimalNumberHandler alloc]
initWithRoundingMode:NSRoundPlain
scale:NSDecimalNoScale
raiseOnExactness:YES
raiseOnOverflow:YES
raiseOnUnderflow:YES
raiseOnDivideByZero:YES];
}
return self;
}
// My division method
-(NSDecimalNumber *)divideNum1:(NSDecimalNumber *)num1 byNum2:(NSDecimalNumber *)num2
{
NSDecimalNumber *result = [num1 decimalNumberByDividingBy:num2
withBehavior:_behavior];
return result;
}
Indeed, dividing by 0 throws an uncaught exception and terminates the program. Ok, that's better. How do I catch the thrown exception, and syntactically, where?
Use
@try
,@catch
ObjC literals. Wrap your division code in@try
block and catch the caught exception in@catch
block.