Errors and Exceptions in Objective C

Undestanding The Difference

There are two types of bad situations in an Objective C program: errors and exceptions. Let us understand the difference between the two.

Error is an unwanted situation that can happen during program execution but you can recover from it. The program memory remains in a consistent state and further execution can continue.

eg1: Attempt to access a network resource timed out or returned 500

eg2: Attempt to write to disk failed due to lack of disk space or permission denied

Handling: You can handle errors any way you want. Show a user message, log it to a file/console or report it to your back-end.

Exception is an unwanted state in your program that should never have occured. Exceptions may corrupt the memory state of the program in a way that prevents further execution.

This can generally occur in two ways:

  1. A logic error that you overlooked is now causing an exception.

    eg: Accessing invalid array elements i.e. index out of bound exception.

    In the above example, you unconsciously made a programming mistake. You couldn’t have handled it because you overlooked it. This will crash the program.

    Handling: Your approach here should be to reproduce and fix it asap.

  2. While execution, your program comes across a situation that your business logic doesn’t permit.

    eg: A database entry that your business logic expects to be always present is missing. For example, let’s say you are writng a program that requires a currency table with currency names, unicode symbols for currencies and country names. The values from this table are to be shown in a drop down in your program UI. You may create a table upon first launch of the program and insert the values. Later you make an sqlite select query but no values are returned.

    This is where confusion may arises because you can recover from such situations by treating them as errors (because the program memory is not corrupted) and displaying a message to the end user. This approach is wrong because it’s an exception at the business logic level.

    Handling: In such cases, you should force crash the program using NSAssert both in development and production phase. This is an aggressive approach geared toward quickly finding and fixing such exceptions rather than disguising them as errors. Besides, treating them as fatal (crash) attributes them the urgency they deserve.

Some Other Tips

  1. Try to avoid using @try/catch in Objective C because experts don’t recommend it.
  2. Formalize an approach to error/exception handling and stick to it. Document your intent in the code. You will be better off finalizing an approach and employing it consistently rather than revisiting this subject again and again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s