iOS 5.0忽略NSSetUncaughtExceptionHandler?

时间:2011-10-16 04:53:45

标签: exception-handling ios5

我注意到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模拟器上运行时,异常处理程序不会被调用(不会将文件写入文件目录,不记录,如果我在处理程序中设置断点则不会触发)

不幸的是,我没有可用于测试的5.0设备,所以我希望有人可以确认它在5.0上已经损坏,因此我可以提交错误报告(或者如果我的代码有问题,请更正我)

2 个答案:

答案 0 :(得分:2)

使用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) 

答案 1 :(得分:0)

我看到了同样的事情。它适用于我的iPad,但不适用于模拟器。如果有人通过电话确认,请随时编辑此答案以反映出来。