Introduction to Error Handling Programming Guide For Cocoa
Every program must deal with errors as they occur at runtime. The program, for example, might not be able to open a file, or perhaps it cannot parse an XML document. Often errors such as these require the program to inform the user about them. And perhaps the program can attempt to get around the problem causing the error.
Cocoa (and Cocoa Touch) offer developers programmatic tools for these tasks: the
NSError class in Foundation and new methods and mechanisms in the Application Kit to support error handling in applications. An
NSError object encapsulates information specific to an error, including the domain (subsystem) originating the error and the localized strings to present in an error alert. With an application there is also an architecture allowing the various objects in an application to refine the information in an error object and perhaps to recover from the error.
You should read this document to understand the
NSError API and architecture and how to use them.
Organization of This Document
Error Handling Programming Guide for Cocoa has the following articles:
Error Objects, Domains, and Codes describes the attributes of an
NSErrorobject, particularly its domain and error code, and discusses the possible contents of an error object’s “user info” dictionary, including localized message strings and underlying errors.
Using and Creating Error Objects explains how to evaluate an error, how to display an error message using an
NSErrorobject, and how to implement methods that return an
NSErrorobject by reference.
Error Responders and Error Recovery describes the Application Kit architecture for passing error objects up a chain of objects in an application, giving each object a chance to customize the error before it is presented. It also discusses the role of the recovery attempter, an object designated to attempt a recovery from an error if the user requests it.
Handling Received Errors discusses how, in the chain of error-responder objects, you handle a received error and customize it.
Recovering From Errors explains the procedure for attempting a user-requested recovery from an error.
The two chapters relevant to iOS are Error Objects, Domains, and Codes and Using and Creating Error Objects.
“The Document Architecture Supports Robust Error Handling" in Document-Based App Programming Guide for Mac offers valuable advice for subclasses that override methods with a by-reference
Types of Dialogs and When to Use Them offers advice on the form and content of alerts in OS X. iOS Human Interface Guidelines offers similar advice for alerts on iOS. You should consult these guidelines before composing your error messages. Also take a look at the following documents discussing areas of Cocoa programming related to error handling and the presentation of error messages:
Assertions and Logging Programming Guide (both platforms)
Dialogs and Special Panels (alerts on OS X)
Sheet Programming Topics (OS X)
Exception Programming Topics discusses how to raise and handle exceptions. Exception Handling in The Objective-C Programming Language describes the compiler directives
@finally, which are used in exception handling.