Objective-C:断言与异常与错误 [英] Objective-C: Assertion vs. Exception vs. Error

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

问题描述

在Cocoa中,我应该使用NSAssert,NSException,NSError?

In Cocoa, when should I use NSAssert, NSException, NSError?

这是我一直在想的:

NSAssert - 当创建用于程序员的任何客户端程序时,自己可以重复检查规则,约定,假设或前提条件和后置条件?

NSAssert - When creating any client program used for the programmers own benefit to double check rules, conventions, assumptions, or pre-conditions and post-conditions?

NSException - 为了使用库的其他程序员创建第三方库时,他们立即知道输入无效的情况?

NSException - When creating a third-party library for the benefit of other programmers that use the library, so that they immediately know when an input is invalid?

NSError - 当与外部系统交互时,获取的数据,如文件,数据库或Web服务,不能保证给我一个结果? p>

NSError - When interfacing with an external system to get data like a file, database, or web service that isn't guaranteed to give me a result?

推荐答案

NSAssert 会在失败时抛出异常。所以NSAssert是有一个简单和容易的方式来写和检查您在代码中所做的任何假设。这不是(在我看来)异常的替代,只是一个捷径。如果断言失败了,那么代码中的错误就会非常严重,程序不应该继续。

An NSAssert will throw an exception when it fails. So NSAssert is there to be short and easy way to write and to check any assumptions you have made in your code. It is not (in my opinion) an alternative to exceptions, just a shortcut. If an assertion fails then something has gone terribly wrong in your code and the program should not continue.

需要注意的一点是,NSAssert不会被编译到你的代码中一个发布版本,因此这通常用于开发期间的健全性检查。我实际上倾向于使用一个总是活动的自定义的assert宏。

One thing to note is that NSAssert will not be compiled into your code in a release build, so this is typically used for sanity checking during development. I actually tend to use a custom assert macro that is always active.

你会 @throw NSException 是在发布版本中,当某些参数无效或者您被错误地调用时,在公共库/接口等方面确实需要它。请注意, @catch 例外并不是真正标准的做法,并继续运行您的应用程序。如果你尝试这些与一些苹果的标准库(例如核心数据)的坏事情可能发生。类似于断言,如果抛出异常,应用通常会相当快地终止,因为这意味着某处存在编程错误。

The times you would @throw your own NSException are when you definitely want it in a release build, and in things like public libraries/interface when some arguments are invalid or you have been called incorrectly. Note that it isn't really standard practice to @catch an exception and continue running your application. If you try this with some of Apple's standard libraries (for example Core Data) bad things can happen. Similar to an assert, if an exception is thrown the app should generally terminate fairly quickly because it means there is a programming error somewhere.

NSErrors 应该在您的库/接口中用于不是编程错误的错误,并且可以从中恢复。您可以向调用者提供信息/错误代码,他们可以干净地处理错误,在适当情况下警告用户,并继续执行。这通常是类似于文件未找到错误或一些其他非致命错误。

NSErrors should be used in your libraries/interfaces for errors that are not programming errors, and that can be recovered from. You can provide information/error codes to the caller and they can handle the error cleanly, alert the user if appropriate, and continue execution. This would typically be for things like a File-not-found error or some other non-fatal error.

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

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