Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5.0 ignoring NSSetUncaughtExceptionHandler?

Tags:

exception

ios5

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).

like image 426
Karl Avatar asked Sep 09 '26 23:09

Karl


1 Answers

Use iOS 6.0 SDK, build and run it on simulator 5.0! It works!

//////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");
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSSetUncaughtExceptionHandler(&handleException);
    [NSException raise:@"Test" format:@"Testing"];

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[NSSetUncaughtExceptionHandlerTestViewController alloc] initWithNibName:@"NSSetUncaughtExceptionHandlerTestViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

//////Terminal log:

2012-10-09 20:06:24.726 NSSetUncaughtExceptionHandlerTest[7815:c07] Handled exception
2012-10-09 20:06:24.727 NSSetUncaughtExceptionHandlerTest[7815:c07] *** Terminating app due to uncaught exception 'Test', reason: 'Testing'
*** First throw call stack:
(0x14a4052 0xea4d0a 0x144ca78 0x144c9e9 0x29d7 0x129d6 0x138a6 0x22743 0x231f8 0x16aa9 0x138efa9 0x14781c5 0x13dd022 0x13db90a 0x13dadb4 0x13daccb 0x132a7 0x14a9b 0x28b2 0x27e5)
terminate called throwing an exception(lldb) 
like image 134
whutdyp Avatar answered Sep 14 '26 15:09

whutdyp