Exceptions and the Cocoa Frameworks
Exceptions in Cocoa are represented by objects of the
NSException class, which is part of the Foundation framework. The methods of this class allow you to create exception objects, raise (throw) exceptions with them, and get the call return addresses related to an exception. The attributes of an
NSException object are the following:
name— a short string that is used to uniquely identify the exception. The name is required.
reason— a longer string that contains a “human-readable” reason for the exception. The reason is required.
An optional dictionary (
userInfo) used to supply application-specific data to the exception handler. For example, if the return value of a method causes an exception to be raised, you could pass the return value to the exception handler through
You may extract the information in an exception object and, if appropriate, present to the user in an alert dialog, perhaps using an
NSError object. See Handling Exceptions for information on this subject.
The Cocoa frameworks require that all exceptions be instances of
NSException or its subclasses. Do not throw objects of other types.
The Cocoa frameworks are generally not exception-safe. The general pattern is that exceptions are reserved for programmer error only, and the program catching such an exception should quit soon afterwards.