Header Ads

Error Handling - Objective C

In Objective C programming, error handling is provided with NSError class variable 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 directory containing application specification information.

NSError

Objective C programs use NSError objects to convey information about runtime  errors that user 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 consist of :

  • Domain : The error domain can be one of the predefined NSError domain or an arbitary 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 = @{ NSLocalizedDescriptionary : 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.MyApplication.ErrorDomain";
             NSString *desc = @"Unable to complete the process";
            NSDictionary *userInfo = [[NSDictionary alloc]intiWithObjectsAndKeys : desc,
             @"NSLocalizedDescriptionKey", NULL];

            *errorPtr = [NSError errorWithDomain : domain code : -101 userInfo : userInfo];
             return @"";
         
          }
   }

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

       error = nil;
       NSString *Name2 = [obj getEmployeeNameForID : 2 withError : &error];
     
      
      if (error)
      {
            NSLog(@"Error finding Name2 :%@", error);
       }
      else
       {
           NSLog(@"Name2 : %@", name2);
        }     return 0;
}



No comments:

Powered by Blogger.