如何实现全球iPhone异常处理? [英] How do you implement global iPhone Exception Handling?

查看:111
本文介绍了如何实现全球iPhone异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在我的iPhone应用程序崩溃,确实抛出一个NSException。崩溃报告是完全不明确的错误是什么,是什么导致它。有没有聪明的方式,我在一个地方设置一个顶级异常处理程序,看看是什么原因?我不能自己复制这个问题,但我的一些测试版用户当然可以。

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.

捕获异常可以通过几种不同的方式完成,但为此,最好的方法会出现要使用NSSetUncaughtExceptionHandler设置异常处理程序。

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.

当应用程序中引发异常时,它由默认异常处理程序处理。这个处理程序只是在应用程序关闭之前向控制台记录一条消息。你可以通过设置你自己的自定义异常处理程序使用上述函数来超越这一点。执行此操作的最佳位置是应用程序委托applicationDidFinishLaunching:方法。

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);
}

不幸的是,与OSX相比,iPhone在生成一个很好的堆栈跟踪。上面的代码将产生一些看似垃圾的输出;但是,您可以通过atos工具运行此输出,并且您应该能够从中生成有用的堆栈跟踪。

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.

另一个选项是按照< a href =http://relme.wordpress.com/2008/12/30/getting-a-useful-stack-trace-from-nsexception-callstackreturnaddresses/ =nofollow noreferrer>本文这将有助于自动生成一个很好的堆栈跟踪。

Another option is to follow the instructions on this article which will help to produce a nice stack trace automatically.

因为这将要发布到beta测试者,你可能需要修补,让它为你工作。

As this is going out to beta testers you may have to tinker about to get it working for you.

您说您无法自己复制问题,只有您的用户。在这种情况下,您可能会发现Apple的这份技术说明有用:

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 :虽然这篇文章仍然包含有用的信息,它包含的一些链接是不可逆转的。建议使用其他帖子的信息。

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屋!

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