---
title: "Objective-C Error Handling"  
description: "Objective-C programs use NSError objects to convey information about runtime errors that users need to be informed about."  
author: "Tarun Kumar"  
published: 2015-07-15  
updated: 2017-12-13  
canonical: https://www.mindstick.com/articles/1825/objective-c-error-handling  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 3 minutes  

---

# Objective-C Error Handling

Previously we learn how to [handle exception](https://www.mindstick.com/forum/162065/how-to-handle-exception-globally-in-dot-net-core) in ObjC : *Objective-C [Exception Handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling)* \

In Objective-C [programming](https://yourviews.mindstick.com/view/88478/vibe-coding-the-rise-of-ai-assisted-programming), [error handling](https://www.mindstick.com/forum/160168/error-handling-in-go) is provided with NSError class available in Foundation framework.

An NSError object encapsulates richer and more extensible error information than is possible using only an error code or error string. The core attributes of an NSError object are an error domain (represented by a string), a domain-[specific error](https://www.mindstick.com/forum/160112/how-to-create-custom-exceptions-in-dot-net-core-for-more-specific-error-handling-scenarios) code and a user info [dictionary](https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp) containing [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) specific information.

NSError

Objective-C programs use NSError objects to convey information about [runtime errors](https://www.mindstick.com/forum/159700/how-to-handle-runtime-errors-in-a-dot-net-core-api) that users need to be informed about. In most cases, a program displays this error information in a dialog or sheet. But it may also interpret the information and either ask the user to attempt to recover from the error or attempt to correct the error on its own

##### NSError Object consists of:

-Domain: The error domain can be one of the predefined NSError domains or an

arbitrary string describing a custom domain and domain must not be nil.

-Code: The error code for the error.

-User Info: The userInfo dictionary for the error and userInfo may be nil.

##### The following example shows how to create a custom error

NSString *domain = @"com.MyCompany.MyApplication.ErrorDomain";

NSString *desc = NSLocalizedString(@"Unable to complete the process", @"");

NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : desc };

NSError *error = [NSError errorWithDomain:domain code:-101 userInfo:userInfo];

##### Here is complete code of the above error sample passed as reference to an pointer

#import< Foundation/Foundation.h>

@interface SampleClass:NSObject

-(NSString *) getEmployeeNameForID:(int) id withError:(NSError **)errorPtr;

@end

@[implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) SampleClass

-(NSString *) getEmployeeNameForID:(int) id withError:(NSError **)errorPtr{ if(id == 1) { return @"Employee Test Name"; } else { NSString *domain = @"com.MyCompany.MyApplication.ErrorDomain"; NSString *desc [=@"Unable](mailto:=@%22Unable) to complete the process"; NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:desc, @"NSLocalizedDescriptionKey",NULL]; *errorPtr = [NSError errorWithDomain:domain code:-101 userInfo:userInfo]; return @""; }}

@end

\
int main(){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; SampleClass *sampleClass = [[SampleClass alloc]init]; NSError *error = nil; NSString *name1 = [sampleClass getEmployeeNameForID:1 withError:&error]; if(error) { NSLog(@"Error finding Name1: %@",error); } else { NSLog(@"Name1: %@",name1); }

error = nil;

NSString *name2 = [sampleClass getEmployeeNameForID:2 withError:&error];

if(error) { NSLog(@"Error finding Name2: %@",error); } else { NSLog(@"Name2: %@",name2); }

[pool drain]; return 0; }

In the above example, we return a name if the id is 1, otherwise we set the user-defined error object.

##### When the above code is compiled and executed, it produces the following result:

2015-07-15 06:11:35.792 demo[26130] Name1: Employee Test Name

2015-07-15 06:11:35.793 demo[26130] Error finding Name2: Unable to complete the process

---

Original Source: https://www.mindstick.com/articles/1825/objective-c-error-handling

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
