Chapter 10. Handling Errors

In This Chapter

  • Learning about different kinds of errors and how you should handle them

  • Using return codes to return status

  • Using exceptions for exceptional errors

  • Learning the proper way to use NSError

You hope that run time errors won't occur, but you know that they will. You try to code defensively, making sure to verify that the variables you are using have the values you expect. You write unit tests to make sure that any possible condition that could occur has been foreseen, and the solution to that problem is built into your application. You know that you can't foresee all problems, however. You know that no matter how hard you work to gird your application against the perils of the real world, as soon as your application is launched into real-world environments with real-world problems, such as limited memory, limited disk space, and users who interrupt your application at the worst time, you're going to run into problems. You're going to experience errors.

Thankfully, Objective-C has a variety of error handling capabilities built into it that you can leverage to write code that is robust, resilient, and stable. You can write applications that, when errors occur, bend rather than break.

In this chapter, I'm going to introduce you to the three major mechanisms built into Objective-C and the Foundation framework that will help you write code that will be brave in the face of these dangers, and which will "do the right thing" when presented with unforeseen ...

Get Objective-C 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.