Objective-C异常 [英] Objective-C Exceptions

查看:86
本文介绍了Objective-C异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了一个iPhone应用程序编程课程。作为课程的一部分,我看到




  • Objective-C使用 @try 指令

  • 系统库不使用异常处理,更喜欢 return nil



我问我是否应该为我写的新代码使用异常处理(例如,如果我写了前端和逻辑代码,所以他们之间的沟通在我手中)但是我被告知不,不应该使用新的代码例外。 (但是他没有详细说明,那么班上班,我以为也许以后的原因会变得很清楚。)



当然这个例外优于返回nil ?您可以捕获特定类型,您不会通过忽略通常成功的函数的返回类型来忽略它们,您可以记录可以记录的文本消息,它们允许您的代码集中在正常情况下,从而更易于阅读。 为什么要使用例外



那么你呢认为?我的教练是否不使用Objective-C异常?如果是这样,为什么?

解决方案

在不自动管理资源的情况下抛出异常是不安全的。这是Cocoa框架(和邻居框架)的情况,因为它们使用手动引用计数。



如果抛出异常,则任何释放打电话给你跳过,堆栈会导致泄漏。只有当您确定您不会恢复时,这才应该限制您的转移,因为所有资源都会在进程退出时返回到操作系统。



不幸的是, code> NSRunLoop 倾向于捕获传播给他们的所有异常,所以如果在事件中抛出,你将恢复到下一个事件。这显然是非常糟糕的。因此,最好不要扔。



如果您使用垃圾回收的Objective-C作为Objective-C表示的任何资源,这个问题就会减少对象将被正确释放。但是,不包含在Objective-C对象中的C资源(如文件描述符或 malloc - 分配的内存)仍然会泄漏。



所以,总而言之,不要抛出。



如前所述,Cocoa API有几个解决方法。返回 nil NSError ** 模式是其中两个。



ARC的说明



ARC用户可以选择启用或禁用完全异常安全。当启用异常安全性时,ARC将生成代码以在其范围被杀死时释放强引用,从而安全地在代码中使用异常。 ARC不会修补外部库,以便在其中启用异常支持,所以即使在程序中启用了异常支持,您也应该小心抛出(尤其是在哪里捕获)。



可以使用 -fobjc-arc-exceptions 启用ARC异常支持,或使用 -fno-objc-arc-exceptions 。默认情况下,它在Objective-C中被禁用,但在Objective-C ++中启用。



默认情况下,Objective-C中的完全异常安全性被禁用,因为Clang作者假定目标-C程序将无法从异常中恢复,并且因为存在与该清理相关联的大代码大小成本和小的性能损失。在Objective-C ++中,另一方面,C ++已经引入了大量的清理代码,人们更有可能真正需要异常安全。



这是所有来自 LLVM网站上的ARC规范


I have just completed an iPhone app programming course. As part of the course, I saw

  • Objective-C provides exception handling using the @try directive
  • The system library does not use exception handling, preferring to return nil

I asked if I should use exception handling for new code I wrote (e.g. if I write both the front-end and logic code, so the communication between them is in my hands) but I was told no, one should not use exceptions for new code. (But he failed to elaborate, then the class moved on, I thought perhaps the reason would become clear later..)

Surely exceptions are superior to return nil? You can catch particular types, you are not tempted to ignore them by ignoring the return type of a function which normally succeeds, you have text messages which can be logged, they allow your code to focus on the normal case and thus be more readable. Why to use exceptions.

So what do you think? Was my trainer right not to use Objective-C exceptions? If so, why?

解决方案

It is unsafe to throw exceptions in circumstances where resources are not automatically managed. This is the case of the Cocoa framework (and neighbor frameworks), as they use manual reference counting.

If you throw an exception, any release call you skip over by unwinding the stack will result in a leak. This should limit you tothrowing only if you're certain that you're not going to recover since all resources are returned to the OS when a process quits.

Unfortunately, NSRunLoops tend to catch all exceptions that propagate to them, so if you throw during an event, you'll resume to the next event. This is, obviously, very bad. Therefore, it's better that you simply don't throw.

This problem is diminished if you use garbage-collected Objective-C, as any resource represented by an Objective-C object will be properly released. However, C resources (such as file descriptors or malloc-allocated memory) that are not wrapped in an Objective-C object will still leak.

So, all in all, don't throw.

The Cocoa API has several workarounds to this, as you mentioned. Returning nil and the NSError** pattern are two of them.

Clarifications for ARC

ARC users can choose to enable or disable full exception safety. When exception safety is enabled, ARC will generate code to release strong references when their scope is killed, making it safe to use exception in your code. ARC will not patch external libraries to enable exception support in them, so you should be careful where you throw (and especially where you catch), even with exception support enabled in your program.

ARC exception support can be enabled with -fobjc-arc-exceptions or disabled with -fno-objc-arc-exceptions. By default, it is disabled in Objective-C but enabled in Objective-C++.

Full exception safety in Objective-C is disabled by default because the Clang authors assume that Objective-C programs will not recover from an exception anyways, and because there is a large code size cost and a small performance penalty associated to that cleanup. In Objective-C++, on the other hand, C++ already introduces a lot of cleanup code, and people are much more likely to actually need exception-safety.

This is all from the ARC specification on the LLVM website.

这篇关于Objective-C异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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