Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Implements | |
Package | com.apple.cocoa.foundation |
Availability | Available in Mac OS X v10.3 and later. |
Companion guide |
NSError encapsulates richer and more extensible error information than is possible using only an error code or error string. The core attributes of an NSError 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. In addition, NSError allows an arbitrary user info dictionary to be specified, 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, the presence of an error should be indicated by other means, for example by returning false
or null
from the method. The method can then optionally return an NSError object by-reference, in order to further describe the error.
Creates an empty NSError.
public NSError
()
Creates an NSError object for domain with the specified error code and the dictionary of arbitrary data userInfo.
public NSError
(String domain, int code, NSDictionary dict)
The domain must not be null
. The userInfo may be null
.
The domain can be one of the predefined NSError domains, or an arbitrary string describing a custom domain.
Returns the receiver’s error code.
public int code
()
Note that errors are domain specific.
Returns a string containing the receiver’s error domain.
public String domain
()
Returns a string containing the localized description of the error.
public String localizedDescription
()
By default this method will attempt to return the object in the user info dictionary for the key LocalizedDescriptionKey
. If the user info dictionary doesn’t contain a value for LocalizedDescriptionKey
, a default string will be constructed from the domain and code.
This method can be overridden by subclasses to present customized error strings.
Returns a string containing the localized explanation of the reason for the error.
public String localizedFailureReason
()
By default this method will attempt to return the object in the user info dictionary for the key LocalizedFailureReasonErrorKey
.
This method can be overridden by subclasses to present customized error strings.
Returns an array containing the localized titles of buttons appropriate for displaying in an alert panel.
public Array localizedRecoveryOptions
()
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
. By default this method will attempt to return the object in the user info dictionary for the key LocalizedRecoveryOptionsErrorKey
. If the user info dictionary doesn’t contain a value for LocalizedRecoveryOptionsErrorKey
, null
is returned and only an OK button is displayed..
This method can be overridden by subclasses to present customized recovery suggestion strings.
Returns a string containing the localized recovery suggestion for the error.
public String localizedRecoverySuggestion
()
This string is suitable for displaying as the secondary message in an alert panel. By default this method will attempt to return the object in the user info dictionary for the key LocalizedRecoverySuggestionErrorKey
. If the user info dictionary doesn’t contain a value for LocalizedRecoverySuggestionErrorKey
, null
is returned.
This method can be overridden by subclasses to present customized recovery suggestion strings.
Returns an object that conforms to the NSErrorRecoveryAttempting
informal protocol.
public Object recoverAttempter
()
The recovery attempter must be an object that can correctly interpret an index into the array returned by localizedRecoveryOptions
. By default this method will attempt to return the object for the user info dictionary for the key RecoveryAttempterErrorKey
. If the user info dictionary doesn’t contain a value for RecoveryAttempterErrorKey
, null
is returned.
Returns an NSDictionary containing the user info associated with the receiver or null
if the user info dictionary has not been set.
public NSDictionary userInfo
()
The following keys may exist in the user info dictionary:
Constant | Description |
---|---|
LocalizedFailureReasonErrorKey | A localized string representation containing the reason for the failure that, if present, will be returned by Available in Mac OS X 10.4 and later. |
LocalizedRecoverySuggestionErrorKey | 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 10.4 and later. |
LocalizedRecoveryOptionsErrorKey | 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 Available in Mac OS X 10.4 and later. |
RecoveryAttempterErrorKey | 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 Available in Mac OS X 10.4 and later. |
The following error domains are predefined:
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)