tags:

views:

34

answers:

1

Example:

@try {
    // 1) do bad stuff that can throw an exception...

    // 2) do some more stuff

    // 3) ...and more...
}
@catch (NSException *e) {
    NSLog(@"Error: %@: %@", [e name], [e reason]);
}

If 1) throws an exception, is the block immediately canceled like a return in a function or a break in a loop? Or will 2) and 3) be processed no matter what happens in 1)?

+2  A: 

If exception occurs then the execution of your block interrupts immediately and @catch section (if it handles appropriate exception type) gets executed.

Sample code:

@try {
    NSArray* arr = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
    NSLog([arr objectAtIndex: 0]);
    NSLog([arr objectAtIndex: 5]);
    NSLog(@"Lala");
}
@catch (NSException * e) {
    NSLog(@"%@, %@", [e name], [e reason]);
}

Output:
1
NSRangeException, *** -[NSCFArray objectAtIndex:]: index (5) beyond bounds (3)
Vladimir