开发者

objective-c iphone programming : try catch exceptions

im using kumulos to have access to a database. this is the code i am talking about :

NSString *location = [[theResults objectAtIndex:0] objectForKey:@"location"];

Now the thing if [theResults objectatindex:0] return "null" it crash everytime so if the user enter something that is not in the database it crash i want to catch this exeption (NSRange exeception).

开发者_如何学Python

Thanks


I think this will work for you without requiring exception handling.

if ([theResults count] > 0) {
    NSString *location = [[theResults objectAtIndex:0] objectForKey:@"location"];
}

I'm assuming that theResults is an NSArray (or subclass).


either you check that [theResults objectAtIndex:0] does not return nil, or you use exception handling

@try {
   NSString *location = [[theResults objectAtIndex:0] objectForKey:@"location"];  
}
@catch (NSRangeEception * e) {
   NSLog(@"catching %@ reason %@", [e name], [e reason]);
}
@finally {
   //something that you want to do wether the exception is thrown or not.
}

I would suggest to study the language at least a little bit, though, or practice with google :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜