| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in Mac OS X v10.0 and later. |
| Companion guide | |
| Declared in | NSException.h |
| Related sample code |
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 is write-protected as an exception. In this sense, the exception is equivalent to an error. Another application might interpret the user’s keypress (for example, Control-C) as an exception: an indication that a long-running process should be aborted.
Note: 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.
+ exceptionWithName:reason:userInfo:
+ raise:format:
+ raise:format:arguments:
– initWithName:reason:userInfo:
– raise
Creates and returns an exception object .
+ (NSException *)exceptionWithName:(NSString *)name reason:(NSString *)reason userInfo:(NSDictionary *)userInfo
The name of the exception.
A human-readable message string summarizing the reason for the exception.
A dictionary containing user-defined information relating to the exception
The created NSException object or nil if the object couldn't be created.
NSException.h
A convenience method that creates and raises an exception.
+ (void)raise:(NSString *)name format:(NSString *)format, ...
The name of the exception.
A human-readable message string (that is, the exception reason) with conversion specifications for the variable arguments that follow.
Variable information to be inserted into the formatted exception reason (in the manner of printf).
The user-defined information is nil for the generated exception object.
NSException.h
Creates and raises an exception with the specified name, reason, and arguments.
+ (void)raise:(NSString *)name format:(NSString *)format arguments:(va_list)argList
The name of the exception.
A human-readable message string (that is, the exception reason) with conversion specifications for the variable arguments in argList.
Variable information to be inserted into the formatted exception reason (in the manner of vprintf).
The user-defined dictionary of the generated object is nil.
NSException.hReturns the call return addresses related to a raised exception.
- (NSArray *)callStackReturnAddresses
An array of NSNumber objects encapsulating NSUInteger values. Each value is a call frame return address. The array of stack frames starts at the point at which the exception was first raised, with the first items being the most recent stack frames.
NSException subclasses posing as the NSException class or subclasses or other API elements that interfere with the exception-raising mechanism may not get this information.
NSException.hInitializes and returns a newly allocated exception object.
- (id)initWithName:(NSString *)name reason:(NSString *)reason userInfo:(NSDictionary *)userInfo
The name of the exception.
A human-readable message string summarizing the reason for the exception.
A dictionary containing user-defined information relating to the exception
The created NSException object or nil if the object couldn't be created.
This is the designated initializer.
NSException.hReturns an NSString object used to uniquely identify the receiver.
- (NSString *)name
NSException.hRaises the receiver, causing program flow to jump to the local exception handler.
- (void)raise
All other methods that raise an exception invoke this method, so set a breakpoint here if you are debugging exceptions. When there are no exception handlers in the exception handler stack, unless the exception is raised during the posting of a notification, this method calls the uncaught exception handler, in which last-minute logging can be performed. The program then terminates, regardless of the actions taken by the uncaught exception handler.
NSException.hReturns an NSString object containing a “human-readable” reason for the receiver.
- (NSString *)reason
NSException.hReturns an NSDictionary object containing application-specific data pertaining to the receiver.
- (NSDictionary *)userInfo
Returns 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.
NSException.hThe string constants for exceptions are listed and described in the "Constants" chapter.
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-01-30)