Mac OS X Reference Library Apple Developer
Search

Introduction to Exception Programming Topics for Cocoa

This document discusses how to raise and handle exceptions: special conditions that interrupt the normal flow of program execution. The Objective-C directives and Foundation API for exceptions are available on iOS and Mac OS X.

Important: You should reserve the use of exceptions for programming or unexpected runtime errors such as out-of-bounds collection access, attempts to mutate immutable objects, sending an invalid message, and losing the connection to the window server. You usually take care of these sorts of errors with exceptions when an application is being created rather than at runtime.

If you have an existing body of code (such as third-party library) that uses exceptions to handle error conditions, you may use the code as-is in your Cocoa application. But you should ensure that any expected runtime exceptions do not escape from these subsystems and end up in the caller’s code. For example, a parsing library might use exceptions internally to indicate problems and enable a quick exit from a parsing state that could be deeply recursive; however, you should take care to catch such exceptions at the top level of the library and translate them into an appropriate return code or state.

Instead of exceptions, error objects (NSError) and the Cocoa error-delivery mechanism are the recommended way to communicate expected errors in Cocoa applications. For further information, see Error Handling Programming Guide.

Organization of This Document

This document contains the following articles:

See Also

For information on originating, handling, and recovering from expected runtime errors, see Error Handling Programming Guide. Also see the related document,Assertions and Logging Programming Guide, for information on the Foundation framework's support for making assertions and logging error information.




Last updated: 2010-02-24

Did this document help you? Yes It's good, but... Not helpful...