捕获iPhone程序的Crash

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://guoxiaoxin.blogbus.com/logs/74067257.html

 

我们可以在delegate的terminateWithException里,写一些垂死挣扎的代码 - 例如保存数据,例如汇报crash情况到服务器.

 

void exceptionHandler(NSException *exception)

{

    id delegate = [[UIApplication sharedApplication] delegate];

    if([delegate respondsToSelector:@selector(terminateWithException:)])

    {

        [delegate performSelector:@selector(terminateWithException:withObject:exception];

    }

}



void signalHandler(int sig, siginfo_t *info, void *context)

{

    // signal exception

    NSException *e = [NSException exceptionWithName:@"signal exception"

                                             reason:[NSString stringWithFormat:@"signal %d", sig]

                                           userInfo:nil];

    exceptionHandler(e);

}



int main(int argc, char *argv[])

{

    // For uncaultexception

    NSSetUncaughtExceptionHandler(&exceptionHandler);

    // For signals

    struct sigaction sigAction;

    sigAction.sa_sigaction = signalHandler;

    sigAction.sa_flags = SA_SIGINFO;

    sigemptyset(&sigAction.sa_mask);

    sigaction(SIGQUIT, &sigAction, NULL);

    sigaction(SIGILL, &sigAction, NULL);

    sigaction(SIGTRAP, &sigAction, NULL);

    sigaction(SIGABRT, &sigAction, NULL);

    sigaction(SIGEMT, &sigAction, NULL);

    sigaction(SIGFPE, &sigAction, NULL);

    sigaction(SIGBUS, &sigAction, NULL);

    sigaction(SIGSEGV, &sigAction, NULL);

    sigaction(SIGSYS, &sigAction, NULL);

    sigaction(SIGPIPE, &sigAction, NULL);

    sigaction(SIGALRM, &sigAction, NULL);

    sigaction(SIGXCPU, &sigAction, NULL);

    sigaction(SIGXFSZ, &sigAction, NULL);

    // startup

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int retVal = UIApplicationMain(argc, argv, @"NBApplication", @"twitBirdAppDelegate");

    [pool release];

    return retVal;

}

crash , 就两种情况,一种是异常,一种是中断.

void exceptionHandler(NSException *exception) 处理异常

void signalHandler(int sig, siginfo_t *info, void *context) 处理中断.

 

 

- (void)terminateWithException:(NSException*)e

{

static BOOL deadway = NO;

if(deadway)

return;

deadway = YES;

NSLog(@"Exception --- %@", e);

[self applicationWillTerminate:[UIApplication sharedApplication]];

deadway = NO;

}

你可能感兴趣的:(iphone开发,crash,iphone,exception,null,signal,服务器)