Error and exception handling with a try/catch block

suggest change

Exceptions represent programmer-level bugs, such as accessing an array element that doesn’t exist.

Errors are user-level issues, such as trying to load a file that doesn’t exist. They are expected during 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents