- Command-Line Arguments
- Objective-C - Error Handling
- Objective-C - Log Handling
- Objective-C - Type Casting
- Objective-C - Typedef
- Objective-C - Preprocessors
- Objective-C - Structures
- Objective-C - Strings
- Objective-C - Pointers
- Objective-C - Arrays
- Objective-C - Numbers
- Objective-C - Blocks
- Objective-C - Functions
- Objective-C - Decision Making
- Objective-C - Loops
- Objective-C - Operators
- Objective-C - Constants
- Objective-C - Variables
- Objective-C - Data Types
- Objective-C - Basic Syntax
- Objective-C - Program Structure
- Objective-C - Environment Setup
- Objective-C - Overview
- Objective-C - Home
Advanced Objective-C
- Obj-C - Memory Management
- Objective-C - Fast Enumeration
- Obj-C - Foundation Framework
- Objective-C - Composite Objects
- Objective-C - Dynamic Binding
- Objective-C - Protocols
- Objective-C - Extensions
- Objective-C - Posing
- Objective-C - Categories
- Objective-C - Data Encapsulation
- Objective-C - Polymorphism
- Objective-C - Inheritance
- Objective-C - Classes & Objects
Objective-C Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Objective-C Error Handpng
In Objective-C programming, error handpng 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 code and a user info dictionary containing apppcation specific information.
NSError
Objective-C programs use NSError objects to convey information about runtime errors 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.MyApppcation.ErrorDomain"; NSString *desc = NSLocapzedString(@"Unable to complete the process", @""); NSDictionary *userInfo = @{ NSLocapzedDescriptionKey : 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 SampleClass -(NSString *) getEmployeeNameForID:(int) id withError:(NSError **)errorPtr { if(id == 1) { return @"Employee Test Name"; } else { NSString *domain = @"com.MyCompany.MyApppcation.ErrorDomain"; NSString *desc =@"Unable to complete the process"; NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:desc, @"NSLocapzedDescriptionKey",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 −
2013-09-14 18:01:00.809 demo[27632] Name1: Employee Test Name 2013-09-14 18:01:00.809 demo[27632] Error finding Name2: Unable to complete the processAdvertisements