Objective-C: Assertion vs. Exception vs. Error

前端 未结 4 1891
南旧
南旧 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:34

    In general, exceptions are used to signal programmer errors — they're things that shouldn't happen. Errors are used to signal error conditions that might come up in the normal operation of the program — user errors, basically, or external conditions that need to be true but might not be. So trying to delete some locked element in a document might be an error, and trying to download a file without an Internet connection would be an error, but trying to access an invalid element in a collection would be an exception.

    Assertions are typically used in testing, and AFAIK are not used as a general error-handling mechanism like the others.

提交回复
热议问题