异常处理 --- 一些垂死挣扎的代码
2012-11-13 15:08
120 查看
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明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;
}
收藏到:Del.icio.us
我们可以在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;
}
收藏到:Del.icio.us
相关文章推荐
- 关于合理使用SpringMVC统一异常处理机制以改善代码风格的一些思考
- 关于合理使用SpringMVC统一异常处理机制以改善代码风格的一些思考
- “System.ArgumentNullException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理
- “System.IndexOutOfRangeException”类型的异常在 Stores.dll 中发生,但未在用户代码中进行处理
- android webview 使用以及一些常见的异常处理
- symbian的应用程序中,如何处理申请内存失败的异常的一些讨论
- eclipse中常见的一些异常对应的处理方法!
- ARM平台上Linux异常处理代码简要分析
- 关于异常的处理一些问题之我见
- MySql存储过程异常处理示例代码分享
- 异常处理,保证代码稳定的必经之步----小话c语言(12)
- ARM-IRQ异常处理代码
- 处理异常的一些思考……
- Halcon 算子异常处理(C++代码)
- ajaxfileupload.js 在别人代码的基础上,自己处理了一些问题,留着备用
- android一些有用的方法,代码,和错误处理总结(持续更新)
- Windows Phone 处理 MessageBox导致的应用异常退出以及使用代码方式退出应用 推荐
- Android-NDK开发之基础--Android JNI实例代码(四)-- JNI中的异常处理实例代码
- 使用try-with-resource改进代码异常处理机制
- python异常处理救命代码