在iPhone应用程序中使用NSException [英] Usage of NSException in iPhone Apps

查看:96
本文介绍了在iPhone应用程序中使用NSException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一位朋友告诉我不要在iPhone应用中使用NSException。他给出的原因是性能瓶颈。但是我不相信它。

One of my friends asked me not to use NSException in iPhone Apps. The reason he gave was "performance bottleneck". But I am not convinced of it.

有人可以确认我应该限制在iPhone App中使用NSException吗?如果您有使用NSException的最佳做法,请同时提供。

Can someone confirm me that we should restrain from using NSException in iPhone App? If you have best practices for using NSException, please provide that too.

更新:

链接要求我们在应用级别使用异常处理。有人做过吗?请给出它的优点以及它可以创造的任何其他性能障碍。

This link requests us to use Exception handling at the app level. Have someone ever done it? Please give the advantages of it and any other performance hitches it could create.

推荐答案

简而言之:

仅使用@ try / @ catch来处理不可恢复的错误。使用@ throw / @ try / @ catch在iOS或Mac OS X上执行类似操作的控制流是绝对不合适的。即使这样,也要仔细考虑是否最好使用异常来指示不可恢复的错误或只是崩溃( call abort());崩溃往往会留下更多的证据。

It is only appropriate to use @try/@catch to deal with unrecoverable errors. It is never appropriate to use @throw/@try/@catch to do control-flow like operations on iOS or Mac OS X. Even then, consider carefully whether you are better off using an exception to indicate an unrecoverable error or simply crashing (call abort()); a crash often leaves behind significantly more evidence.

例如,除非你的目标是抓住它们,否则用它来捕捉越界异常是不合适的。以某种方式报告错误,然后 - 通常 - 崩溃,或者至少警告用户您的应用程序处于不一致状态并可能丢失数据。

For example, it would not be appropriate to use for catching out-of-bounds exceptions unless your goal is to catch them and somehow report the error, then -- typically -- crash or, at the least, warn the user that your app is in an inconsistent state and may lose data.

未定义通过系统框架代码抛出的任何异常的行为。


你能解释一下系统
框架代码抛出的任何
异常的行为是未定义的。在
详细信息?

Can you explain the "Behavior of any exception thrown through system framework code is undefined." in detail?

当然。

系统框架使用任何异常被认为是致命的,不可恢复的错误的设计;所有意图和目的的程序员错误。这条规则的例外数量非常有限(呵呵)。

The system frameworks use a design where any exception is considered to be a fatal, non-recoverable, error; a programmer error, for all intents and purposes. There are a very limited number of exceptions (heh) to this rule.

因此,在实施过程中,系统框架无法确保一切都必须得到适当的清理。抛出一个异常,通过系统框架代码。根据定义,正例是不可恢复的,为什么要支付清理费用?

Thus, in their implementation, the system frameworks will not ensure that everything is necessarily properly cleaned up if an exception is tossed that passes through system framework code. Sine the exception is, by definition, unrecoverable, why pay the cost of cleanup?

考虑这个调用堆栈:

your-code-1()
    system-code()
        your-code-2()

即代码调用系统代码的代码,调用更多代码(一种非常常见的模式,尽管调用堆栈明显更深)。

I.e. code where your code calls into system code which calls into more of your code (a very common pattern, though the call stacks are obviously significantly deeper).

如果 your-code-2 抛出异常,异常通过 system-code 表示行为未定义; system-code 可能会也可能不会使您的应用程序处于未定义,可能崩溃或数据丢失的状态。

If your-code-2 throws an exception, that the exceptions passes over system-code means the behavior is undefined; system-code may or may not leave your application in an undefined, potentially crashy or data-lossy, state.

或者,更强烈:您不能在 your-code-2 中抛出异常,期望您可以在中捕获并处理它-code-1

Or, more strongly: You can't throw an exception in your-code-2 with the expectation that you can catch and handle it in your-code-1.

这篇关于在iPhone应用程序中使用NSException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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