Objective-c 中返回错误

发布于 2024-08-05 21:38:03 字数 101 浏览 6 评论 0原文

我对 Objective-C 很陌生,我开始想知道处理和捕获错误的常见/标准/正确方法是什么?

似乎可以使用 NSError 来做到这一点,这是一个好主意还是对可可的劫持?

Im newish to objective-c and am starting to wonder what is the common/standard/proper way for handling and catching errors?

It seems like it might be possible to use NSError to do this, is that a good idea or a hijack of cocoa?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凝望流年 2024-08-12 21:38:03

我很确定这就是 NSError 类的作用 - 提供有关错误的详细信息。您将看到的最常见的模式是采用指向 NSError 对象的指针的方法,如下所示:

- (id)doSomethingWithArgument:(id)arg error:(NSError **)error

该方法为执行某些操作的结果返回一些值(或可能为 nil),但是如果调用失败将在传递的指针处放置一个 NSError 对象,其中包含有关失败的详细信息。您的文档负责指定方法遇到错误时返回的内容。

我想到的另一个方法是@throw-@catch块;然而,在 Objective-C 中 @throw 异常的计算量可能相当大,并且通常只建议在真正的异常情况下这样做。

编辑:哇,事实证明很多人对@throw异常有非常强烈的看法。总结一下关于这个问题的(非常有帮助的)评论:

  • 抛出异常通常应该处理程序员错误(应该永远发生的情况,等等);异常不应该用于普通的错误处理。相反,请使用上面演示的 error 方法或发布 NSNotification 实例。
  • 如果您最终大量使用 @throw/@catch 块,请非常小心它们周围的逻辑。 Objective-C 提供了很多方法来分离方法以在其他线程中运行,或延迟执行等。在编写代码时要非常小心地考虑所有这些可能性。

最后,另一个非常有效的观点:

  • 如果您确实使用传递给方法的 error 对象,则返回值应该指出它。不要尝试同时执行这两项操作(返回部分有效的对象设置错误对象)。

I'm pretty sure that's what the NSError class is there to do - give details about errors. The most common pattern you'll see is a method that takes a pointer to an NSError object, as in:

- (id)doSomethingWithArgument:(id)arg error:(NSError **)error

The method returns some value (or possibly nil) for the result of doing something, but if the call failed will place an NSError object at the pointer passed with details about the failure. Your documentation is responsible for specifying what gets returned if the method does encounter an error.

The other method that comes to mind is the @throw-@catch block; however, in Objective-C @throwing an exception can be rather computationally expensive, and it's usually only recommended to do so in truly exceptional situations.

Edit: wow, turns out a lot of people have really strong opinions about @throwing exceptions. To sum up the (quite helpful) commentary on the issue:

  • Throwing exceptions should most often deal with programmer error (situations that should never happen, and the like); exceptions should not be used for ordinary error handling. Instead, use the error method demonstrated above or post instances of NSNotification.
  • If you do wind up making extensive use of @throw/@catch blocks, be very careful about the logic surrounding them. Objective-C provides a lot of ways to detach methods to run in other threads, or delay execution, etc. Be very careful to account for all those possibilities when you write your code.

Finally, another very valid point:

  • If you do use the error object passed to a method, the return value should indicate it. Don't try to do both (return a partially valid object and set the error object).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文