Objective-C: Assertion vs. Exception vs. Error

前端 未结 4 1887
南旧
南旧 2020-12-12 10:55

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

Here\'s what I\'ve been thinking:

NSAssert - When creating any client program us

4条回答
  •  清歌不尽
    2020-12-12 11:19

    The convention in Cocoa is that an exception indicates a programmer error. A lot of code, including framework code, is not designed to work properly after an exception is thrown.

    Any sort of error that should be recoverable is represented by an NSError. There’s also a system for presenting NSErrors to the user. As you say, this is mostly useful for fallible external resources.

    Conceptually, an assertion is a statement that a given predicate always evaluates to true; if it doesn’t, the program is broken. While its behaviour can be modified, the NSAssert family is by default a convenient way of throwing NSInternalInconsistencyExceptions (with the option of turning them off in release builds).

提交回复
热议问题