3 ms·
I reviewed the exception section, and was surprised to see no mention of this tidbit from Apple's developer documentation: "You should reserve the use of excep
by ankhmoop 18y ago
I reviewed the exception section, and was surprised to see no mention of this tidbit from Apple's developer documentation:
"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."
The reason for this is fairly straight-forward -- very little Objective-C code is exception-safe, it's relatively difficult to make it exception safe, and blindly traversing the call stack to the nearest exception handler will result in leaked resources and associated failures.
The correct way to handle errors in Objective-C is with NSError return values, which properly supports (among other things) reporting localized error messages and possible solutions to the user using standard UI, and optionally taking action on that solution using a recovery handler.
The Apple documentation on these subjects is quite comprehensive.
(edit) I should also mention that as of Objective-C 2.0, the language (Obj-C) and Apple's Foundation library are effectively linked. The compiler contains numerous direct dependencies on the Foundation library for the purpose of implementing properties, 'fast' enumeration, etc. While it used to be possible to claim that the languages and library were distinct (nothing prevented you from using a base class library other than Foundation), this is no longer necessarily the case. Any use of raw Objective-C requires the existence of Foundation-compatible protocol implementations.
Thus, any modern discussion of Objective-C 2.0 error handling is arguably lacking without mention of Foundation's facilities.
Likewise, discussion of Objective-C 1.0 error handling would have extended beyond the domain of Objective-C were it to include mention of the Foundation library's facilities.