简体   繁体   English

如何在objective-c和xcode中启用异常处理

[英]How to enable exception handling in objective-c and xcode

EDIT: Issue has been solved(partially):It is a simulator bug. 编辑:问题已经解决(部分):这是一个模拟器错误。 I've compiled and tested this on two devices with iOS 3.1.3 and 4.0. 我已经在iOS 3.1.3和4.0的两个设备上编译和测试了这个。 The exception was handled correctly. 正确处理了异常。 Be careful, the simulator is your enemy! 小心,模拟器是你的敌人!

this is driving me crazy. 这真让我抓狂。 I don't know how to enable exception handling in my project. 我不知道如何在我的项目中启用异常处理。 Look at the code and debugger output below. 查看下面的代码和调试器输出。

My Goal is to catch the exception, not correcting the code so the exception is handled and the app doesn't crash. 我的目标是捕获异常,而不是更正代码,以便处理异常并且应用程序不会崩溃。

I'm using XCode 3.2.3, iPhone SDK 4 final. 我正在使用XCode 3.2.3,iPhone SDK 4 final。 I have just created a simple view based iPhone App to test this. 我刚刚创建了一个基于简单视图的iPhone App来测试它。

I have looked in my project settings and yes the switch "Enable Objective-C Exceptions" is checked. 我查看了我的项目设置,然后选中了“启用Objective-C Exceptions”开关。 I am using GCC 4.2. 我正在使用GCC 4.2。

When I look at the build process in detail the compiler flag -fno-objc-exceptions is not within the list of arguments! 当我详细查看构建过程时,编译器标志-fno-objc-exceptions不在参数列表中!

What am I missing here? 我在这里错过了什么?

Thanks in advance Nick 在此先感谢尼克

NSArray * foo = [[NSArray alloc] init];

@try {
   NSLog(@"trying...");  
   [foo objectForKey:@"yeah"];
}
@catch (NSException * e) {
   NSLog(@"catching %@ reason %@", [e name], [e reason]);
}
@finally {
   NSLog(@"finally");
}

leads to 导致

trying...

-[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x5d5f780

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x5d5f780'

*** Call stack at first throw:
(
 0   CoreFoundation                      0x02393919 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x024e15de objc_exception_throw + 47
 2   CoreFoundation                      0x0239542b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
 3   CoreFoundation                      0x02305116 ___forwarding___ + 966
 4   CoreFoundation                      0x02304cd2 _CF_forwarding_prep_0 + 50
...
)
terminate called after throwing an instance of 'NSException'

Whether the catch nor the finally block is ever reached. 是否达到了捕获量和最终块数。

Quote from How do I catch global exceptions? 引用如何捕获全局异常? : " objc_exception_throw is not an exception. It is the function that throws Objective-C exceptions. Similarly, EXC_ARITHMETIC is not an Objective-C exception; it is a Mach (kernel) exception, meaning that your app tried to do something completely invalid. – Peter Hosey May 14 at 9:14" :“ objc_exception_throw不是异常。它是抛出Objective-C异常的函数。同样, EXC_ARITHMETIC不是Objective-C异常;它是一个Mach(内核)异常,意味着你的app试图做一些完全无效的事情。 - Peter Hosey 5月14日9:14“

That thread does have a link to a solution for your problem though, it appears. 该线程确实有一个指向您的问题的解决方案的链接,但它出现了。 The link goes to http://www.restoroot.com/Blog/2008/10/20/crash-reporter-for-iphone-applications-part-2/ which looks a little risky, but if it works, it might be worth it for you. 链接到http://www.restoroot.com/Blog/2008/10/20/crash-reporter-for-iphone-applications-part-2/看起来有点风险,但如果有效,可能是值得为你。

There are bug reports related to this, eg: http://www.openradar.me/8081169 (posted earlier this month) 有与此相关的错误报告,例如: http//www.openradar.me/8081169 (本月早些时候发布)

(Updated to summarize information from comments below.) (更新以总结以下评论中的信息。)

If I understand your problem right. 如果我理解你的问题吧。

Your Try/ catch block is working correctly. 您的Try / catch块工作正常。

It is trying to run your code, and catches an error. 它试图运行您的代码,并捕获错误。 You need to decide what to do when it catches an error and code for it within the block. 您需要决定在块中捕获错误并为其编写代码时要执行的操作。 I normally do that in the CATCH part. 我通常在CATCH部分那样做。 As the finally bit will execute regardless of an exception or not being thrown. 因为最终位将执行而不管异常或不被抛出。

Your example code is catching the NSException exception but not the one being thrown, NSInvalidArgumentException . 您的示例代码捕获NSException异常,但不捕获正在抛出的异常NSInvalidArgumentException You might have better luck if you look for that specific exception. 如果你寻找那个特定的例外,你可能会有更好的运气。

NSArray * foo = [[NSArray alloc] init];

@try {
   NSLog(@"trying...");  
   [foo objectForKey:@"yeah"];
}
@catch (NSInvalidArgumentException *e) {
   NSLog(@"Invalid argument called.");
}
@catch (NSException * e) {
   NSLog(@"catching %@ reason %@", [e name], [e reason]);
}
@finally {
   NSLog(@"finally");
}

I don't have any way of testing it myself right now, though. 不过,我现在没有办法自己测试。

See http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocExceptionHandling.html for more information. 有关详细信息,请参阅http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocExceptionHandling.html

要在Iphone / Ipad中处理异常处理,您必须将@try @catch块放入更多详细信息,请参阅异常处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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