views:

39

answers:

1

Could someone tell me how I can capture a NSParseErrorException?

The Situation: my app downloads a small .plist file. I convert this into dictionary using the string method -propertylist. This normally works fine. I check for a connection before going to retrieve the file, so it works fine if I've got a connection to the internet, and works fine when I don't.

However, I discovered a use case that crashes. If I'm at location that requires authetication before connecting to the internet (at Starbucks, say) what's being returned by the app isn't a plist and the attempt to parse it causes the application to crash.

So is there a way to transform my code so that the NSParseErrorException is caught and rather than crashing the program I can just skip over this piece of code?

NSDictionary *temp = [myDownloadString propertyList];

I tried doing this

if ([myDownloadString propertyList]==NSParseErrorException){
//do something
}

but that didn't work.

+1  A: 

Exceptions are a special kind of error that isn't directly returned from methods; an exception breaks the chain of execution entirely. To handle it, you have to put the code that might throw it in what's called a try/catch block, like this:

NSDictionary * temp = nil;
@try {
    temp = [myDownloadString propertyList];
} @catch (NSParseErrorException * exception) {
    // do something
}

Exception handling is a large topic with other stuff going on (finally blocks, etc)-- you should check out some further documentation eg here.

quixoto
Works like a charm. Thanks.
Martin