简体   繁体   中英

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).

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 :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM