在Objective-C中使用异常 [英] Using exceptions in Objective-C

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

问题描述

我刚刚接触到Objective-C世界。在研究某些iOS / Mac应用程序时,我注意到的是,很少使用try -catch,如果使用的话。
例如在Java中,几乎所有的时间都被使用。
为什么在Objective-C中这么常见?

解决方案

有很多SDK方法一个 NSError ** 参数,并返回 BOOL 。为了表示错误,他们返回false并通过错误参数反馈错误以传达信息。



使用例外 ,但一般情况下其中在运行时级别有一些失败 - 例如某些对象无法处理选择器。虽然这可能与我刚刚写的相反,但异常倾向于表示设计中的错误,而不是运行时错误。



NSError ** 成语是所有您需要的事情,如失败的URL连接,数据转换等,存在错误条件,但程序真的不应该被彻底杀死。



开始阅读: 错误处理编程指南


I'm new to Objective-C world. What I have noticed while studying some iOS/Mac apps is that try -catch is rarely used, if used at all. For example in Java it is used almost all the time. Why isn't it so common in Objective-C ?

解决方案

There are a lot of SDK methods that take an NSError** parameter and return BOOL. To indicate an error they return false and feed an error back through the error parameter to communicate info.

Exceptions are used, but generally for cases in which there is some failure at the runtime level - e.g. some object can't handle a selector. Although it may seem contrary to what I just wrote, exceptions tend to indicate an error in design rather than a runtime error.

The NSError** idiom is all you need for things such as failed URL connections, data conversions, etc, where an error condition exists but a program really shouldn't be killed outright.

Start reading: Error Handling Programming Guide

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

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