codescracker


objective c

Objective-C Error Handling



« Previous Tutorial Next Tutorial »


Error handling is provided with the NSError class available in the Foundation framework in Objective-C language.

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 the NSError object are an error domain (that is represented by string), a domain-specific error code and a user info dictionary containing application specific information

NSError

NSError objects is used to convey the information about the runtime errors, that users to be informed about. In most of the cases, an Objective-C program displays this error information in a dialog/sheet. But it may also interpret the information and either ask to the user to attempt to recover from error/attempt to correct the error on their own.

NSError objects in Objective-C consists of:

Name Meaning
code error code for the error
domain error domain can be one of the predefined NSError domains or an arbitrary string, describing the custom domain and domain must not be NULL
user info userInfo dictionary for the error and userInfo may be nil

Objective-C Error Handling Example

Here is an example, shows that how to create a custom error in Objective-C:

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 the complete version of the above error sample passed as the reference to an pointer in Objective-C . This example program, illustrates about log handling in Objective-C practically.

/* Objective-C Error Handling - Example Program */
		
#import <Foundation/Foundation.h>
@interface SampleClass:NSObject
-(NSString *) getEmployeeNameForID:(int) id withError:(NSError **)errorPtr;
@end
@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 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;
}

Here, in the above example program, we return a name if the id is 1, otherwise we set the user-defined error object. Here is the output of the above Objective-C program

2014-10-03 13:18:37.932 demo[27632] Name1 = Employee Test Name
2014-10-03 13:18:37.932 demo[27632] Error finding Name2 = Unable to complete the process

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test