Exception handling iOS 5.0是否忽略NSSetUncaughtExceptionHandler?

Exception handling iOS 5.0是否忽略NSSetUncaughtExceptionHandler?,exception-handling,ios5,Exception Handling,Ios5,我注意到iOS 5中有一些奇怪的行为:NSSetUncaughtExceptionHandler()似乎不再做任何事情了 我有以下代码: static NSString* documentsDirectory() { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count]

我注意到iOS 5中有一些奇怪的行为:NSSetUncaughtExceptionHandler()似乎不再做任何事情了

我有以下代码:

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"];

    ...
}
以上代码适用于运行4.x或更早版本的任何设备或模拟器。但是,当我在5.0模拟器上运行它时,不会调用异常处理程序(不会将文件写入documents目录,不会记录,如果在处理程序中设置断点,也不会触发)


不幸的是,我没有5.0设备可供测试,所以我希望有人能确认它在5.0上坏了,这样我就可以提交错误报告(或者纠正我,如果我的代码有问题)。

我也看到了同样的情况。它可以在我的iPad上运行,但不能在模拟器上运行。如果有人在电话中确认,请随意编辑此答案以反映这一点。

使用iOS 6.0 SDK,在模拟器5.0上构建并运行它!它起作用了

//////代码:

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;
}
//////终端日志:

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) 

更新:刚接触到一台iOS 5设备,这个问题似乎只影响模拟器,而不是设备。