Objective-C Language Error Handling Error & Exception handling with try catch block


Exceptions represent programmer-level bugs like trying to access an array element that doesn’t exist.

Errors are user-level issues like trying load a file that doesn’t exist. Because errors are expected during the normal execution of a program.


    NSArray *inventory = @[@"Sam",
    int selectedIndex = 3;
    @try {
        NSString * name = inventory[selectedIndex];
        NSLog(@"The selected Name is: %@", name);
    } @catch(NSException *theException) {
        NSLog(@"An exception occurred: %@", theException.name);
        NSLog(@"Here are some details: %@", theException.reason);
    } @finally {
        NSLog(@"Executing finally block");


An exception occurred: NSRangeException

Here are some details: *** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]

Executing finally block