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

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.

Example:

    NSArray *inventory = @[@"Sam",
                           @"John",
                           @"Sanju"];
    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");
    }

OUTPUT:

An exception occurred: NSRangeException

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

Executing finally block



Got any Objective-C Language Question?