NSError

NSError is the unsung hero of the Foundation framework. Passed gallantly in and out of perilous method calls, it is the messenger by which we are able to contextualize our failures. No news is good news, when it comes to such matters, but passing a nil pointer to an NSError ** isn’t going to do you any favors.

nshipster.com/nserror/

NSError is Cocoa's way to handle errors without exceptions. It tells about failures with a code, domain, and userInfo dictionary.

The code shows the type of error in its domain. Domains stop codes from overlapping. UserInfo adds details like localizedDescription, failureReason, and recoverySuggestion.

To make an NSError, use errorWithDomain:code:userInfo:.

Methods that might fail take an NSError ** parameter. Pass &error; if it fills, there is a problem.

When you cause an error, fill the caller's NSError **, maybe wrap a lower error in NSUnderlyingErrorKey.

Network errors use NSURLErrorDomain with codes like -1001 for timeout. The article lists many, from general to SSL and file issues.

NSError helps pass error info clearly, better than raw codes.


Category:

Year: