Exception not caught with try/except around runloop spin (GCD-added main loop block)

Originator:nategriswold
Number:rdar://13222338 Date Originated:14-feb-2013
Status:Open Resolved:
Product:iPhone SDK Product Version:4.6 (4H127)
Classification:Crash Reproducible:Always
 
Exceptions thrown in gcd-queued main queue blocks do not result in the 'except' of a try-except handler to be called.

 When wrapping a runloop spin in a try except block, one might expect any exceptions thrown in any code called as a result of the spin to be caught be a try except handler around the spin. This is not the case for blocks added to the run loop using dispatch_async(dispatch_get_main_queue(), block)

In other words, the program crashes and "Caught" is not printed in the below.

    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSException exceptionWithName:@"ExceptionException" reason:@"Exception" userInfo:nil] raise];
    });
    
    @try {
        
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    } @catch (NSException *e) {
        NSLog(@"Caught");
    }

Steps to Reproduce:

Run the attached project

Expected Results:

No crash. "Caught" is printed to the console. 

Actual Results:

Crash

Regression:

Notes:

Exceptions thrown as a result of code in a queued performSelector:withObject:afterDelay:  are caught gracefully. A commented line is provided to demonstrate this.

14-Feb-2013 09:29 PM Nate Griswold:
'ExceptionNotCaught.zip' was successfully uploaded

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!