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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3147398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:29:40  来源:igfitidea点击:

How to enable exception handling in objective-c and xcode

iphoneobjective-cxcodegccexception-handling

提问by Nick Weaver

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. The exception was handled correctly. Be careful, the simulator is your enemy!

编辑:问题已解决(部分):这是一个模拟器错误。我已经在装有 iOS 3.1.3 和 4.0 的两台设备上编译并测试了它。异常被正确处理。小心,模拟器是你的敌人!

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. I have just created a simple view based iPhone App to test this.

我使用的是 XCode 3.2.3,iPhone SDK 4 final。我刚刚创建了一个简单的基于视图的 iPhone 应用程序来测试这个。

I have looked in my project settings and yes the switch "Enable Objective-C Exceptions" is checked. I am using GCC 4.2.

我查看了我的项目设置,是的,选中了“启用 Objective-C 异常”开关。我正在使用 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.

是否曾到达 catch 或 finally 块。

采纳答案by Kalle

Quote from How do I catch global exceptions?: "objc_exception_throwis not an exception. It is the function that throws Objective-C exceptions. Similarly, EXC_ARITHMETICis 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(内核)异常,这意味着您的应用程序试图做一些完全无效的事情。 – Peter霍西 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, e.g.: http://www.openradar.me/8081169(posted earlier this month)

有与此相关的错误报告,例如:http: //www.openradar.me/8081169(本月早些时候发布)

(Updated to summarize information from comments below.)

(更新以总结以下评论中的信息。)

回答by James Sumners

Your example code is catching the NSExceptionexception but not the one being thrown, NSInvalidArgumentException. You might have better luck if you look for that specific exception.

您的示例代码正在捕获NSException异常,但没有捕获异常,NSInvalidArgumentException. 如果你寻找那个特定的例外,你可能会有更好的运气。

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.htmlfor more information.

有关更多信息,请参阅http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocExceptionHandling.html

回答by markhunte

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. As the finally bit will execute regardless of an exception or not being thrown.

它正在尝试运行您的代码,并捕获错误。您需要决定在捕获错误时要执行的操作并在块中为其编码。我通常在 CATCH 部分这样做。因为无论是否抛出异常,finally 位都会执行。