Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Exceptions

Exceptions are not a normal way of handling errors in Objective-C. From Exception Programming Topics (developer.apple.com):

The Cocoa frameworks are generally not exception-safe. The general pattern is that exceptions are reserved for programmer error only, and the program catching such an exception should quit soon afterwards.

In short, exceptions are not for handling recoverable errors in Objective-C. Exceptions are for handling those things that should never happen and which should terminate the program. This is similar to NSAssert, and in fact NSAssert is implemented as an exception.

Objective-C has language-level support for exceptions using directives such as @throw and @catch, but you generally should not use these. There is seldom a good reason to catch exceptions except at the top level of your program, which is done for you with the global exception handler. If you want to raise an exception to indicate a programming error, it’s best to use NSAssert to raise an NSInternalInconsistencyException, or create and raise your own NSException object. You can build these by hand, but I recommend +raise:format: for simplicity.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial