I am a Python developer using exceptions. In many places, I found that using exceptions is not so wise here, and I did my best to convert it to NSErrors when necessary. but then I come across this:
NSMutableArray *results; for (NSDictionary *dict in dicts) { // Memory management code omitted SomeModel *model = [[SomeModel alloc] init]; model.attr1 = [[dict objectForKey:@"key1"] integerValue]; model.attr2 = [[dict objectForKey:@"key2"] integerValue]; model.attr3 = [[dict objectForKey:@"key3"] integerValue]; model.attr4 = [[dict objectForKey:@"key4"] integerValue]; [results addObject:model]; }
with some objects in a dict containing NSNull, which will result in an โunrecognized selectorโ exception. In this case, I want to completely abandon this database. My first instinct is to wrap all the contents of a for block in a catch @@ catch block:
NSMutableArray *results; for (NSDictionary *dict in dicts) { @try { SomeModel *model = [[SomeModel alloc] init]; model.attr1 = [[dict objectForKey:@"key1"] integerValue]; model.attr2 = [[dict objectForKey:@"key2"] integerValue]; model.attr3 = [[dict objectForKey:@"key3"] integerValue]; model.attr4 = [[dict objectForKey:@"key4"] integerValue]; [results addObject:model]; } @catch(NSException *exception) {
But is this a good approach? I can't come up with a solution without repeating the checks for each variable, which is a really ugly IMO. I hope there are alternatives to this that are not met with me. Thanks in advance.
source share