Inherits from: NSObject
Conforms to: NSCoding
NSCopying
NSObject (NSObject)
Declared in: Foundation/NSException.h
NSException is used to implement exception handling and contains information about an exception. An exception is a special condition that interrupts the normal flow of program execution. Each application can interrupt the program for different reasons. For example, one application might interpret saving a file in a directory that's write-protected as an exception. In this sense, the exception is equivalent to an error. Another application might interpret the user's keypress (i.e., Control-C) as an exception: an indication that a long-running process should be aborted.
The exception handling mechanism uses longjmp to control the flow of execution. Any code written for an application that uses exception handling is therefore subject to the restrictions associated with this functionality. See your compiler documentation for more information on the longjmp function. |
Similarly, you can leave the local exception handler (the section of code between NS_HANDLER and NS_ENDHANDLER) by raising an exception or simply "falling off the end".
In the code example above, the same exception, localException, is raised again at the end of the local handler, allowing an encompassing exception handler to take some additional action. Exception handlers can be nested so that an exception raised in an inner domain can be treated by the local exception handler and any number of encompassing exception handlers. The following diagram illustrates the use of nested exception handlers, and is discussed in the text that follows.
An exception raised within Method3's domain causes execution to jump to its local exception handler. In a typical application, this exception handler checks the object localException to determine the nature of the exception. For exception types that it recognizes, the local handler responds and then may send raise to localException to pass notification of the exception to the handler above, the handler in Method2. (An exception that's re-raised appears to the next higher handler just as if the initial exception had been raised within its own exception handling domain.) Method2's exception handler does the same and then re-raises the exception to Method1's handler. Finally, Method1's handler re-raises the exception. Since there's no exception handling domain above Method1, the exception is transferred to the uncaught exception handler as described below.
If an exception is not caught by any handler, it's intercepted by the uncaught exception handler, a function set by NSSetUncaughtExceptionHandler() and returned by NSGetUncaughtExceptionHandler().
The default uncaught exception handler logs a message in the console and exits the program. However, for Application Kit programs, the message is logged with the Workspace Manager's console window (if the application was launched by the Workspace Manager) or to a Terminal window (if the application was launched from the shell).
You can change the default behavior by changing the uncaught exception handler using NSSetUncaughtExceptionHandler().
Rhapsody predefines a number of exception names. These exception names are defined in NSException.h. For example:
NSGenericException
NSRangeException
NSInvalidArgumentException
NSMallocException
You can catch any of these exceptions from within your exception handler by comparing the exception's name with these predefined names. Note that all predefined exceptions begin with the prefix "NS", so you should avoid using the same prefix when creating new exception names.
- NSCoding
- - encodeWithCoder:
- - initWithCoder:
- NSCopying
- - copyWithZone:
- Creating and raising an NSException
- + exceptionWithName:reason:userInfo:
- + raise:format:
- + raise:format:arguments:
- - initWithName:reason:userInfo:
- - raise
- Querying an NSException
- - name
- - reason
- - userInfo
+ (NSException *)exceptionWithName:(NSString *)name
reason:(NSString *)reason
userInfo:(NSDictionary *)userInfo
See Also: - initWithName:reason:userInfo:, - name, - reason, - userInfo
+ (void)raise:(NSString *)name format:(NSString
*)format,...
printf()
. The
user-defined information is nil
.See Also: + raise:format:arguments:, - raise
+ (void)raise:(NSString *)name format:(NSString
*)format arguments:(va_list)argList
vprintf()
. The user-defined
information is nil
.See Also: + raise:format:, - raise
- (NSString *)description
See Also: - reason
- (id)initWithName:(NSString *)name
reason:(NSString *)reason
userInfo:(NSDictionary *)userInfo
See Also: + exceptionWithName:reason:userInfo:, - initWithName:reason:userInfo:
- (NSString *)name
See Also: + exceptionWithName:reason:userInfo:, - initWithName:reason:userInfo:
- (void)raise
See Also: + raise:format:, + raise:format:arguments:
- (NSString *)reason
See Also: - description, + exceptionWithName:reason:userInfo:, - initWithName:reason:userInfo:
- (NSDictionary *)userInfo
nil
if
no application-specific data exists. As an example, if a method's
return value caused the exception to be raised, the return value
might be available to the exception handler through this method.See Also: + exceptionWithName:reason:userInfo:, - initWithName:reason:userInfo: