开发者

Capturing a NSParseErrorException

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 p开发者_高级运维iece of code?

NSDictionary *temp = [myDownloadString propertyList];

I tried doing this

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

but that didn't work.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜