Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Assertion vs. Exception vs. Error

In Cocoa, when should I use NSAssert, NSException, NSError?

Here's what I've been thinking:

NSAssert - When creating any client program used for the programmers own benefit to double check rules, conventions, assumptions, or pre-conditions and post-conditions?

NSException - When creating a third-party library for the benefit of other programmers that use the library, so that they immediately know when an input is invalid?

NSError - When interfacing with an external system to get data like a file, database, or web service that isn't guaranteed to give me a result?

like image 866
Tobias Avatar asked Feb 15 '11 21:02

Tobias


People also ask

What is the difference between assertion and exception?

The key differences between exceptions and assertions are: Assertions are intended to be used solely as a means of detecting programming errors, aka bugs. By contrast, an exception can indicate other kinds of error or "exceptional" condition; e.g. invalid user input, missing files, heap full and so on.

When should you use an assertion over an exception?

Assertions should be used to check something that should never happen, while an exception should be used to check something that might happen. For example, a function might divide by 0, so an exception should be used, but an assertion could be used to check that the harddrive suddenly disappears.

Why are exceptions better than assertions?

Exceptions versus assertions Use assert statements to test for conditions during development that should never be true if all your code is correct. There's no point in handling such an error by using an exception, because the error indicates that something in the code has to be fixed.

Does assert throw exception?

If the user inputs something incorrect, throw an exception. If the input is correct but something's still wrong, throw an assert. If the user inputs something incorrect, that should be expected, not an exception.


1 Answers

An NSAssert will throw an exception when it fails. So NSAssert is there to be short and easy way to write and to check any assumptions you have made in your code. It is not (in my opinion) an alternative to exceptions, just a shortcut. If an assertion fails then something has gone terribly wrong in your code and the program should not continue.

One thing to note is that NSAssert will not be compiled into your code in a release build, so this is typically used for sanity checking during development. I actually tend to use a custom assert macro that is always active.

The times you would @throw your own NSException are when you definitely want it in a release build, and in things like public libraries/interface when some arguments are invalid or you have been called incorrectly. Note that it isn't really standard practice to @catch an exception and continue running your application. If you try this with some of Apple's standard libraries (for example Core Data) bad things can happen. Similar to an assert, if an exception is thrown the app should generally terminate fairly quickly because it means there is a programming error somewhere.

NSErrors should be used in your libraries/interfaces for errors that are not programming errors, and that can be recovered from. You can provide information/error codes to the caller and they can handle the error cleanly, alert the user if appropriate, and continue execution. This would typically be for things like a File-not-found error or some other non-fatal error.

like image 161
Mike Weller Avatar answered Sep 21 '22 08:09

Mike Weller