您如何实施全球iPhone异常处理? [英] How do you implement global iPhone Exception Handling?
问题描述
处理这种问题的一个聪明的方法是什么?
看起来你在这里问两个问题:如何设置顶级异常处理程序;以及如何处理确定根本原因的问题。
捕获异常可以通过几种不同的方式完成,但是最好的方法会出现要使用NSSetUncaughtExceptionHandler设置异常处理程序。
当您的应用程序中引发异常时,它将由默认异常处理程序处理。在处理程序关闭之前,这个处理程序只能将消息记录到控制台。您可以通过使用上述函数设置自己的自定义异常处理程序来超越此功能。执行此操作的最佳位置将在应用程序委托applicationDidFinishLaunching:方法中。
- (void)applicationDidFinishLaunching:(UIApplication *)应用程序
{
NSSetUncaughtExceptionHandler(& myExceptionHandler);
}
一旦你设置了一个自定义处理程序,你将要扩展帮助您确定原因的默认输出。
void myExceptionHandler(NSException * exception)
{
NSArray * stack = [exception callStackReturnAddresses];
NSLog(@Stack trace:%@,stack);
}
不幸的是,与OSX相比,iPhone在生产一个不错的堆栈方面显得相当有限跟踪。上面的代码会产生一些看似垃圾的输出;然而,您可以通过atos工具运行此输出,您应该可以从中生成有用的堆栈跟踪。
另一个选项是按照< a href =http://relme.wordpress.com/2008/12/30/getting-a-useful-stack-trace-from-nsexception-callstackreturnaddresses/ =noreferrer>这篇文章将有助于自动生成一个很好的堆栈跟踪。
由于这是出来的beta测试人员,您可能需要修补它才能为您工作。
您说您自己无法复制问题,只有您的用户。在这种情况下,您可能会发现Apple的技术说明有用:
http://developer.apple.com/iphone/library/technotes/tn2008/tn2151.html
更新:虽然这篇文章仍然包含有用的信息,但它包含的一些链接是不可逆转的死亡。建议您使用此替代信息中的信息。
I have one crash in my iPhone application that does throw an NSException. The crash reports are completely ambiguous in where the error is and what exactly is causing it. Is there a smart way for me to set a top level exception handler somewhere to see what is causing it? I can't replicate the problem myself, but a few of my beta users certainly can.
What's a smart way to handle a problem of this nature?
It seems like you are asking two questions here: how to set a top level exception handler; and how to deal with the issue of determining what the root cause is.
Catching the exception can be done in a few different ways, but for this the best approach would appear to be to set an exception handler using NSSetUncaughtExceptionHandler.
When an exception is raised in your app, it is handled by a default exception handler. This handler does nothing more than log a message to the console before the app closes. You can over-ride this by setting you own custom exception handler using the function stated above. The best place to do this would be in the app delegate applicationDidFinishLaunching: method.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSSetUncaughtExceptionHandler(&myExceptionHandler);
}
Once you've set a custom handler, you'll want to expand on the default output in helping you determine what the cause is.
void myExceptionHandler(NSException *exception)
{
NSArray *stack = [exception callStackReturnAddresses];
NSLog(@"Stack trace: %@", stack);
}
Unfortunately compared to OSX the iPhone appears quite limited in respect to producing a nice stack trace. The code above will produce some seemingly junk output; however, you can run this output through the atos tool, and you should be able to generate a useful stack trace from it.
Another option is to follow the instructions on this article which will help to produce a nice stack trace automatically.
As this is going out to beta testers you may have to tinker about to get it working for you.
You say that you've not been able to replicate the problem yourself, only your users. In this case you may find this technical note from Apple useful:
http://developer.apple.com/iphone/library/technotes/tn2008/tn2151.html
UPDATE: While this post still contains useful info, some of the links it contains are dead irrevertably. It is advised to use the info from this alternative post.
这篇关于您如何实施全球iPhone异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!