在为iphone编写API时,哪些错误处理的最佳做法是什么? [英] What are best practices for error handling when writing an API for the iphone?

查看:82
本文介绍了在为iphone编写API时,哪些错误处理的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在为iphone开发人员编写一个API,我们不知道最佳做法是用于异常处理。我们调查了 NSError ,标准POSIX方式, NSException



大多数API使用的约定是什么?哪个是最客观C友好的?

解决方案

异常编程主题简介


重要:您应该预留使用异常用于编程或意外的运行时错误,例如超出范围的收集访问,尝试变异不可变对象,发送无效消息,并丢失与窗口服务器的连接。您通常在创建应用程序而不是在运行时处理这些错误。


...




代替异常,错误对象(NSError)和Cocoa错误传递机制是在Cocoa应用程序中传达预期错误的推荐方法。有关更多信息,请参阅可可式的错误处理编程指南


所以据了解,只有当事情是非常错误的时候才使用异常。否则,使用 NSError 对象。


We are writing an API for iphone developers and we don't know what the best practice is for exception handling. We looked into NSError, standard POSIX way, NSException

What is the convention that most APIs use? Which is the most "Objective-C friendly"?

解决方案

From the Introduction to Exception Programming Topics:

Important: You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server. You usually take care of these sorts of errors with exceptions when an application is being created rather than at runtime.

...

Instead of exceptions, error objects (NSError) and the Cocoa error-delivery mechanism are the recommended way to communicate expected errors in Cocoa applications. For further information, see Error Handling Programming Guide For Cocoa.

So as I understand it, only use exceptions when something is fatally wrong. Otherwise, use NSError objects.

这篇关于在为iphone编写API时,哪些错误处理的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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