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

查看:20
本文介绍了如何实现全局 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.

另一种选择是按照 这篇文章这将有助于自动生成一个很好的堆栈跟踪.

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:

https://developer.apple.com/library/content/technotes/tn2151/_index.html

更新:虽然这篇文章仍然包含有用的信息,但它包含的一些链接已经不可逆转地死了.建议使用 this 替代帖子中的信息.

UPDATE: While this post still contains useful info, some of the links it contains are dead irreversibly. It is advised to use the info from this alternative post.

这篇关于如何实现全局 iPhone 异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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