Header Ads

Exception Handling - Objective C

Exception handling is made available in Objective C with foundation class NSException.

Exception handling is implemented with the following blocks:
  • @try - This blocks tries to execute a set of statements.
  • @catch  - This block tries to catch the exception in try block.
  • @finally - This block contains set of statements that always execute.

#import <Foundation/Foundation.h>
int main()
{
          NSMutableArray *arrobj = [[NSMutableArray alloc] init];
          @try
           {
                       NSString *strobj = [arrobj objectAtIndex : 10];
            } 
           @catch (NSException *exception)
            {
                   NSLog(@"%@", exception . name);
                   NSLog(@"Reason : %@", exception . Reason);
            }
           @finally
           {
                    NSLog(@"@@finaly Always Executes");
            }
           return 0;}


 In the above program, instead of the program terminating due to the exception , it contains with the subsequent program since we have used exception handling.


No comments:

Powered by Blogger.