尝试catch最终没有捕捉异常 [英] try catch final is not catching exception

查看:140
本文介绍了尝试catch最终没有捕捉异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从通讯录中获取电话号码返回到我的应用程序时,如果错误地取得一些地址或除电话号码之外的任何内容,则try catch final中的代码会提供以下内容,但不能捕获想法是提前感谢:

While on return to my app from getting phone number from address book, if by mistake i fetch some address or any thing except for phone number, the code in the try catch final gives the following but doesn't catch it, any idea is appreciated in advance:

  *** -[CFDictionary length]: message sent to deallocated instance 0x6a4db70

代码:

strContact = (NSString *)phone; CFRelease(phone);
// NSLog(@"%@", strContact);
CFRelease(multi);
name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSRegularExpression *regex = [[[NSRegularExpression alloc] initWithPattern:@"[a-zA-Z]" options:0 error:NULL] autorelease];
// Assuming you have some NSString myString.
NSUInteger matches = [regex numberOfMatchesInString:strContact options:0 range:NSMakeRange(0, [strContact length])];


推荐答案

好的,我想我现在明白你的问题。这只是我花了一段时间。

OK, I think I understand your question now. It just took me a while.

所以,我假设你已经启用了僵尸对象(NSZombieEnabled),你想知道为什么你不能捕捉到这个错误 - 发送一个消息给一个僵尸 - 一个@ try / @ catch结构。

So, I'm assuming you have Zombie Objects enabled (NSZombieEnabled) and you're wondering why you can't catch this error --sending a message to a zombie-- with a @try/@catch structure.

简短的答案是NSZombies不要抛出一个Objective-C异常(一个你可以这样抓住)这不是真的要点,因为你实际上不会运行NSZombieEnabled的应用程序。这是一个在调试器或仪器中使用的调试工具。

The short answer is NSZombies don't throw an Objective-C exception (one that you can catch this way). There wouldn't really be a point to that, since you wouldn't actually ship an app with NSZombieEnabled anyway. This is meant to be a debugging tool that you use at the debugger or within Instruments.

在您的实际应用中,当您关闭NSZombie's时,仍然不会这是一个例外,因为你只会得到一个EXE_BAD_ACCESS,这是一个UNIX信号(这个级别不是这种类型的异常)。

In your actual app, when you turn off NSZombie's, there still wouldn't be an exception to catch for this, since you would just get an EXE_BAD_ACCESS, which is a UNIX signal (not this type of an exception at this level).

最后一点是Objective-C异常(您可以 @ try / @ catch)的类型不推荐用于您希望能够恢复的错误。或者换句话说,这只是为了致命的错误,你可以做一些清理,然后让你的应用程序仍然崩溃。

The last point is that Objective-C exceptions (the type that you can @try/@catch) are not recommended for errors that you expect to be able to recover from. Or in other words, this is meant for fatal errors only, where you might do some cleanup and then allow your app to still crash.

正确的方法来做你尝试做的是实际测试不同的类型或不同的值,你可能会得到,然后适当地处理每个案例。你可以使用正常的if / else条件或这样的事情来做到这一点。如果需要,您可以测试 nil 的值,您可以使用查看某个对象是否为特定类[someObj isKindOfClass:[MyExpectedClass class ]]

The right way to do what you're trying to do is to actually test for different types or different values that you might except to get and then handle each case appropriately. You can do this using normal if/else conditionals or something of that sort. You can test values for nil if you need to and you can see if an object is of a particular class by using [someObj isKindOfClass:[MyExpectedClass class]]

我希望我能正确理解问题?

I hope I understood the question correctly?

这篇关于尝试catch最终没有捕捉异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆