Search code examples
iphoneobjective-ccocoa-touchnsnumbernsdecimalnumber

Getting NSDecimalNumberOverflowException Error


I'm getting an error at line: if (! [self.event.managedObjectContext save:&error]). It only happens when the user does not enter another number into the textField, so it is 0.

- (void)viewWillAppear:(BOOL)animated
{
    self.textField.text = 0;   
    [super viewWillAppear:YES];
}

- (void)addProperty
{
    NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%@", self.textField.text]];
    event.carPayment = decimal;

    NSError *error = nil;
    if (! [self.event.managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
    NSLog(@"%@", error);
}

Error: 2011-09-21 20:31:28.101 Calculator[2391:707] Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  NSDecimalNumber overflow exception with userInfo (null)

2011-09-21 20:31:28.144 Calculator[2391:707] *** Terminating app due to uncaught exception 'NSDecimalNumberOverflowException', reason: 'NSDecimalNumber overflow exception'

Solution

  • I can't tell exactly what you are trying to do, but how about using try-catch syntax to handle the exception and display a message to the user that the value is not permitted to be 0. This assumes it really is not permitted to be 0 or makes no sense for it to be 0. If it does make sense for it to be 0, then try-catch is probably not what you are after.