I am new to iPhone/Objective-C development, I am successfully parsing XML with NSXMLParser but I can’t get exceptions to work properly. I’d like to use exceptions to deal with unexpected XML.
I’m wrapping the code for creating the NSXMLParser object and sending the setDelegate and parse messages to the object inside a @try @catch block, catching @NSException.
If I put NSAssert(FALSE, @"error) inside the @try block, the exception is caught properly. If, however, I have an NSAssert fail inside the delegate calls (eg, didStartElement, didEndElement, foundCharacters), then the program dies (in iPhone Simulator, haven’t tried device yet). The debugger stack trace shows the assertion was raised into an exception but it doesn’t pull back out into the top level code where the @try block is around the [parser parse] message call. Instead I get “Terminating app due to uncaught exception.”
Please let me know if this is a known problem or if I’m doing something silly here.
Thanks — Alex
Some code to make more concrete; no attempt to make this code correct for memory/releases/etc.
@implementation XMLTester
+(void)runXMLTester
{
BOOL success = FALSE;
XMLTester *tester = [[XMLTester alloc] init];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=KSFO"]];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:tester];
@try {
//NSAssert(FALSE, @"error"); // this assertion works fine
success = [parser parse];
}
@catch (NSException * e) {
success = FALSE;
NSLog(@"Exception caught %@: %@", [e name], [e reason]);
}
@finally {
NSLog(@"runXMLTester @finally block hit");
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Starting element %@", elementName);
NSAssert(FALSE, @"error"); // this assertion does not work - does not hit @try block around parse message
}
According to Bill Bumgarner, catching exceptions in the iPhone Simulator doesn’t work correctly. Your best bet is to stop using exceptions here, as it’s not really appropriate anyway. You should be calling
-[NSXMLParser abortParsing]instead.