I’ve noticed some strange behavior in iOS 5: NSSetUncaughtExceptionHandler() doesn’t seem to do anything anymore.
I have the following code:
static NSString* documentsDirectory()
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
static void handleException(NSException *exception)
{
// Write something to the documents dir
NSString* path = [documentsDirectory() stringByAppendingPathComponent:@"crashlog.txt"];
NSString* str = @"Handled exception";
NSError* err;
if(![str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&err])
{
NSLog(@"Failed to write to file");
}
NSLog(@"Handled exception");
}
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSSetUncaughtExceptionHandler(&handleException);
[NSException raise:@"Test" format:@"Testing"];
...
}
The above code works on any device or simulator running 4.x or earlier. However, when I run it on the 5.0 simulator, the exception handler doesn’t get called (doesn’t write the file to the documents dir, doesn’t log, and doesn’t trigger if I set a breakpoint inside the handler).
Unfortunately, I don’t have a 5.0 device available for testing, so I’m hoping someone could confirm that it’s broken on 5.0 so I can file a bug report (or correct me, if there’s a problem with my code).
Use iOS 6.0 SDK, build and run it on simulator 5.0! It works!
//////Code:
//////Terminal log: