简体   繁体   English

针对iPhone / Objective-C的try-catch异常处理实践

[英]Try-catch exception handling practice for iPhone/Objective-C

Apologies if this question has already been answered somewhere else, but I could not find any decisive answer when searching on it: 如果这个问题已经在其他地方得到解答,请道歉,但在搜索时我找不到任何决定性的答案:

I'm wondering when try-catch blocks are to be used in objective-c iPhone applications. 我想知道什么时候try-catch块将用于objective-c iPhone应用程序。 Apple's "Introduction to the Objective-C Programming Language" state that exceptions are resource intensive and that one should "not use exceptions for general flow-control, or simply to signify errors." Apple的“Objective-C编程语言简介”指出,异常是资源密集型的,并且应该“不使用例外进行一般流控制,或者只是表示错误”。 From reading a few related questions here I also gather that people are not often using this method in practice. 通过阅读这里的一些相关问题,我还得知人们并不经常在实践中使用这种方法。

So I guess the question is: what are the situations when it's appropriate to use try-catch blocks when developing for iPhone/Objective-C and when should they absolutely NOT be used? 所以我想问题是:在为iPhone / Objective-C开发时使用try-catch块是否合适的情况是什么时候应该绝对不使用?

For example, I could use them to catch beyond bounds and other exceptions when working with objects in arrays. 例如,在处理数组中的对象时,我可以使用它们来捕获超出边界和其他异常。 I have a method which performs are few tasks with objects that are passed on in a number of arrays. 我有一个方法,执行很少的任务与在多个数组中传递的对象。 The method returns nil if an error has occurred and a try-catch block could help me catch exceptions. 如果发生错误,该方法返回nil,try-catch块可以帮助我捕获异常。 However, I could of course simply write little if-tests here and there to ensure that I, for instance, never try to access an index beyond an arrays bounds. 但是,我当然可以在这里和那里简单地编写一些if-tests,以确保我,例如,永远不会尝试访问超出数组边界的索引。 What would you do in this situation? 在这个情况下,你会怎么做?

Thanks a lot! 非常感谢!

It is only appropriate to use @try/@catch to deal with unrecoverable errors. 只使用@ try / @ catch来处理不可恢复的错误。 It is never appropriate to use @throw/@try/@catch to do control-flow like operations. 使用@ throw / @ try / @ catch来执行类似操作的控制流是不合适的。

In particular, 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. 特别是,除非你的目标是捕获它们并以某种方式报告错误,否则用于捕获越界异常是不合适的 - 然后 - 通常 - 崩溃或至少警告用户你的应用程序处于不一致状态,可能会丢失数据。

Behavior of any exception thrown through system framework code is undefined. 通过系统框架代码抛出的任何异常的行为都是未定义的。

Your if-test to do bounds checking is a far more appropriate solution. 您进行边界检查的if-test是一个更合适的解决方案。

@bbum's answer is absolutely correct (and he would know the answer better than most). @ bbum的答案是绝对正确的(他会比大多数人更了解答案)。 To elaborate a bit... 详细说明......

In Cocoa, you should generally avoid using exceptions ( @try/@catch[/@finally] ) for flow control. 在Cocoa中,通常应避免使用异常( @try/@catch[/@finally] )进行流控制。 As you mention, exceptions carry an unusually large cost (compared to run-times such as JVM or the CLR optimized for exception use). 正如您所提到的,异常带来了非常大的成本(与运行时相比,例如JVM或针对异常使用而优化的CLR)。 Furthermore, most of the Cocoa frameworks are not exception safe. 此外,大多数Cocoa框架都不是例外。 Thus, throwing an exception through Cocoa framework code is dangerous and will likely cause odd, difficult to diagnose, and catastrophic (think possible data loss) bugs in your app. 因此,通过Cocoa框架代码抛出异常是危险的,并且可能会导致应用程序中的奇怪,难以诊断和灾难性(认为可能的数据丢失)错误。

Instead of using exceptions, Cocoa code uses NSError to signal error conditions that are recoverable within the application. Cocoa代码使用NSError来表示应用程序中可恢复的错误条件,而不是使用异常。 Exceptions are used to signal conditions from which your application cannot recover. 例外用于表示应用程序无法恢复的条件。 Thus a UI component requesting an out-of-bounds position in a model array could be signaled with an error (presented to the user with a reason their request could not be completed) while attempting to access an out-of-bounds position given an index that you think should be valid signals an exceptional condition where you app is in an inconsistent state and should probably die as soon as possible before it can do more damage. 因此,在尝试访问给定的越界位置时,可以用错误(向用户呈现其请求无法完成的原因)发信号通知请求模型阵列中的越界位置的UI组件。你认为应该有效的索引表示你的应用程序处于不一致状态并且应该在它可以造成更多伤害之前尽快死亡的异常情况。

NSParameterAssert , for example signals with an NSException when an assertion fails. NSParameterAssert ,例如断言失败时带有NSException信号。

So when should you use exceptions or @try/@catch ? 那么什么时候应该使用异常或@try/@catch If you're using a C/C++ library that makes use of exceptions, you should catch those exceptions before they can get thrown back through Cocoa code. 如果您正在使用一个使用异常的C / C ++库,那么您应该在它们被Cocoa代码抛回之前捕获这些异常。 Similarly, if you are serious about consistency of state within your application, you should raise an exception as soon as you detect that your state is inconsistent (and unrecoverable). 同样,如果您认真对待应用程序中状态的一致性, 则应在检测到状态不一致(且不可恢复)时立即引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM