Objective C Error Handling - Objective C

What is Error Handling in Objective-C?

  • The NSError class in Foundation framework provides error handling in Objective-C programming language.
  • This NSError object encapsulates richer and more extensible error information that is possible using only an error code or error string.
  • Here, 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.

NSError

NSError objects are used to convey information about runtime errors that users need to be informed about in Objective-C programming language. It displays the error information in a dialog or sheet. But it can also interpret the information and asks the user to attempt to recover from the error or attempt to correct the error on its own.

NSError Object consists of:

  • Domain: The error domain can be one of the predefined NSError domains or an arbitrary string describing a custom domain and domain must not be nil.
  • Code: The error code for the error.
  • User Info: The userInfo dictionary for the error and userInfo may be nil.

The following example shows how to create a custom error:

The below code shows the above error sample passed as reference to a pointer:

From the above example, if the id is 1 then we return a name, otherwise we set the user-defined error object.

On compiling and executing the above code, the following result is produced:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Objective C Topics