Chapter 10. Error and exception handling


This chapter covers
  • Dealing with NSError
  • Creating your own NSError objects
  • When and when not to use exceptions


Things go wrong. That’s no surprise. And things go wrong in the software you build. Maybe the server that hosts the API you want to talk to isn’t reachable, or the XML file you need to parse is corrupt, or the file you want to open doesn’t exist. You need to prepare for such cases and handle them gracefully, which means not crashing and giving the user an appropriate message.

Cocoa Touch and Objective-C offer two ways of dealing with errors: NSError and exceptions. NSError is the preferred and advisable way to deal with expected errors (like an unreachable host). Exceptions are meant ...

Get Objective-C Fundamentals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.