Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.2 with Safari 1.0 installed. Available in Mac OS X v10.2.7 and later. Available in iPhone OS 2.0 and later. |
Companion guide | |
Declared in | NSError.h NSURLError.h |
Related sample code |
An NSError
object encapsulates richer and more extensible error information than is possible using only an error code or error string. The core attributes of an NSError
object are an error domain (represented by a string), a domain-specific error code and a user info dictionary containing application specific information.
Several well-known domains are defined corresponding to Mach, POSIX, and OSStatus
errors. Foundation error codes are found in the Cocoa error domain and documented in the Foundation Constants Reference. In addition, NSError
allows you to attach an arbitrary user info dictionary to an error object, and provides the means to return a human-readable description for the error.
NSError
is not an abstract class, and can be used directly. Applications may choose to create subclasses of NSError
to provide better localized error strings by overriding localizedDescription
.
In general, a method should signal an error condition by—for example—returning NO
or nil
rather than by the simple presence of an error object. The method can then optionally return an NSError
object by reference, in order to further describe the error.
– localizedDescription
– localizedRecoveryOptions
– localizedRecoverySuggestion
– localizedFailureReason
Creates and initializes an NSError
object for a given domain and code with a given userInfo
dictionary.
+ (id)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict
The error domain—this can be one of the predefined NSError
domains, or an arbitrary string describing a custom domain. domain must not be nil
.
The error code for the error.
The userInfo
dictionary for the error. userInfo may be nil
.
An NSError
object for domain with the specified error code and the dictionary of arbitrary data userInfo.
NSError.h
Returns the receiver’s error code.
- (NSInteger)code
The receiver’s error code.
Note that errors are domain specific.
NSError.h
Returns the receiver’s error domain.
- (NSString *)domain
A string containing the receiver’s error domain.
NSError.h
Returns an NSError
object initialized for a given domain and code with a given userInfo
dictionary.
- (id)initWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict
The error domain—this can be one of the predefined NSError
domains, or an arbitrary string describing a custom domain. domain must not be nil
.
The error code for the error.
The userInfo
dictionary for the error. userInfo may be nil
.
An NSError
object initialized for domain with the specified error code and the dictionary of arbitrary data userInfo.
This is the designated initializer for NSError
.
NSError.h
Returns a string containing the localized description of the error.
- (NSString *)localizedDescription
A string containing the localized description of the error.
By default this method returns the object in the user info dictionary for the key NSLocalizedDescriptionKey
. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey
, a default string is constructed from the domain and code.
This method can be overridden by subclasses to present customized error strings.
NSError.h
Returns a string containing the localized explanation of the reason for the error.
- (NSString *)localizedFailureReason
A string containing the localized explanation of the reason for the error. By default this method returns the object in the user info dictionary for the key NSLocalizedFailureReasonErrorKey
.
This method can be overridden by subclasses to present customized error strings.
NSError.h
Returns an array containing the localized titles of buttons appropriate for displaying in an alert panel.
- (NSArray *)localizedRecoveryOptions
An array containing the localized titles of buttons appropriate for displaying in an alert panel. By default this method returns the object in the user info dictionary for the key NSLocalizedRecoveryOptionsErrorKey
. If the user info dictionary doesn’t contain a value for NSLocalizedRecoveryOptionsErrorKey
, nil
is returned.
The first string is the title of the right-most and default button, the second the one to the left of that, and so on. The recovery options should be appropriate for the recovery suggestion returned by localizedRecoverySuggestion
. If the user info dictionary doesn’t contain a value for NSLocalizedRecoveryOptionsErrorKey
, only an OK button is displayed.
This method can be overridden by subclasses to present customized recovery suggestion strings.
NSError.h
Returns a string containing the localized recovery suggestion for the error.
- (NSString *)localizedRecoverySuggestion
A string containing the localized recovery suggestion for the error. By default this method returns the object in the user info dictionary for the key NSLocalizedRecoverySuggestionErrorKey
. If the user info dictionary doesn’t contain a value for NSLocalizedRecoverySuggestionErrorKey
, nil
is returned.
The returned string is suitable for displaying as the secondary message in an alert panel.
This method can be overridden by subclasses to present customized recovery suggestion strings.
NSError.h
Returns an object that conforms to the NSErrorRecoveryAttempting
informal protocol.
- (id)recoveryAttempter
An object that conforms to the NSErrorRecoveryAttempting
informal protocol. By default this method returns the object for the user info dictionary for the key NSRecoveryAttempterErrorKey
. If the user info dictionary doesn’t contain a value for NSRecoveryAttempterErrorKey
, nil
is returned.
The recovery attempter must be an object that can correctly interpret an index into the array returned by localizedRecoveryOptions
.
NSError.h
Returns the receiver's user info dictionary.
- (NSDictionary *)userInfo
The receiver's user info dictionary, or nil
if the user info dictionary has not been set.
NSError.h
These keys may exist in the user info dictionary.
extern NSString *NSLocalizedDescriptionKey; extern NSString *NSErrorFailingURLStringKey; const NSString *NSFilePathErrorKey; const NSString *NSStringEncodingErrorKey; const NSString *NSUnderlyingErrorKey; const NSString *NSURLErrorKey; const NSString *NSLocalizedFailureReasonErrorKey; const NSString *NSLocalizedRecoverySuggestionErrorKey; const NSString *NSLocalizedRecoveryOptionsErrorKey; const NSString *NSRecoveryAttempterErrorKey;
NSLocalizedDescriptionKey
The corresponding value is a localized string representation of the error that, if present, will be returned by localizedDescription
.
Available in Mac OS X v10.2 and later.
Declared in NSError.h
.
NSErrorFailingURLStringKey
The corresponding value is the URL that caused the error. This key is only present in the NSURLErrorDomain
.
Available in Mac OS X v10.2 with Safari 1.0 installed.
Available in Mac OS X v10.2.7 and later.
Available in iPhone OS 2.0 and later.
Declared in NSURLError.h
.
NSFilePathErrorKey
Contains the file path of the error.
The corresponding value is an NSString
object.
Available in Mac OS X v10.4 and later.
Declared in NSError.h
.
NSStringEncodingErrorKey
The corresponding value is an NSNumber
object containing the NSStringEncoding
value.
Available in Mac OS X v10.4 and later.
Declared in NSError.h
.
NSUnderlyingErrorKey
The corresponding value is an error that was encountered in an underlying implementation and caused the error that the receiver represents to occur.
Available in Mac OS X v10.3 and later.
Declared in NSError.h
.
NSURLErrorKey
The corresponding value is an NSURL
object.
Available in Mac OS X v10.4 and later.
Declared in NSError.h
.
NSLocalizedFailureReasonErrorKey
The corresponding value is a localized string representation containing the reason for the failure that, if present, will be returned by localizedFailureReason
.
This string provides a more detailed explanation of the error than the description.
Available in Mac OS X v10.4 and later.
Declared in NSError.h
.
NSLocalizedRecoverySuggestionErrorKey
The corresponding value is a string containing the localized recovery suggestion for the error.
This string is suitable for displaying as the secondary message in an alert panel.
Available in Mac OS X v10.4 and later.
Declared in NSError.h
.
NSLocalizedRecoveryOptionsErrorKey
The corresponding value is an array containing the localized titles of buttons appropriate for displaying in an alert panel.
The first string is the title of the right-most and default button, the second the one to the left, and so on. The recovery options should be appropriate for the recovery suggestion returned by localizedRecoverySuggestion
.
Available in Mac OS X v10.4 and later.
Declared in NSError.h
.
NSRecoveryAttempterErrorKey
The corresponding value is an object that conforms to the NSErrorRecoveryAttempting
informal protocol.
The recovery attempter must be an object that can correctly interpret an index into the array returned by recoveryAttempter
.
Available in Mac OS X v10.4 and later.
Declared in NSError.h
.
NSError.h
The following error domains are predefined.
const NSString *NSPOSIXErrorDomain; const NSString *NSOSStatusErrorDomain; const NSString *NSMachErrorDomain;
NSPOSIXErrorDomain
POSIX/BSD errors
Available in Mac OS X v10.2 and later.
Declared in NSError.h
.
NSOSStatusErrorDomain
Mac OS 9/Carbon errors
Available in Mac OS X v10.2 and later.
Declared in NSError.h
.
NSMachErrorDomain
Mach errors
Available in Mac OS X v10.2 and later.
Declared in NSError.h
.
Additionally, the following error domain is defined by Core Foundation:
CFStreamErrorDomain | Defines constants for values returned in the domain field of the CFStreamError structure. |
NSError.h
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)