Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 25 / 68

Custom errors (categorising with NSError)

This lesson covers expressing your own kinds of error with NSError in Objective-C, with the aim of distinguishing them more specifically.

Representing error information with NSError is the usual Objective-C practice. A domain (the error's category name) plus a code tells the kinds apart.

The sample code defines a category name in the constant ValidationErrorDomain, and a checkAge() function passes error information back to the caller through an NSError **error — a pointer to a pointer. That double-pointer form is the standard Objective-C error-handling pattern.

A common early stumble is what NSError ** means. It is the mechanism that lets the function write into the pointer the caller handed over, and it takes a while to feel natural.

In professional work, being able to tell "invalid input" apart from "the network failed" is what lets you write a suitable response to each.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)