`
836811384
  • 浏览: 548975 次
文章分类
社区版块
存档分类
最新评论

NSSetUncaughtExceptionHandler异常捕获

 
阅读更多

NSSetUncaughtExceptionHandler、

利用 NSSetUncaughtExceptionHandler,当程序异常退出的时候,可以先进行处理,然后做一些自定义的动作,比如下面一段代码,就是网上有人写的,直接在发生异常时给某人发送邮件,

void UncaughtExceptionHandler(NSException *exception) {

NSArray *arr = [exception callStackSymbols];

NSString *reason = [exception reason];

NSString *name = [exception name];

NSString *urlStr = [NSString stringWithFormat:@"mailto://xxx@sina.com.cn?subject=bug报告&body=感谢您的配合!<br><br><br>"

"错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",

name,reason,[arr componentsJoinedByString:@"<br>"]];

NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:url];


//或者直接用代码,输入这个崩溃信息,以便在console中进一步分析错误原因

NSLog(@"1heqin, CRASH: %@", exception);

NSLog(@"heqin, Stack Trace: %@", [exception callStackSymbols]);

}

然后在delegate文件里面- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数里面添加NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);


如果不使用上面的处理方式,崩溃时会直接显示下面内容:

2013-07-16 18:46:05.962 GONONO[12638:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 2]'

*** First throw call stack:

(0x1c91012 0x10cee7e 0x1c46b44 0x2f69 0x10e2705 0x162c0 0x16258 0xd7021 0xd757f 0xd66e8 0x45cef 0x45f02 0x23d4a 0x15698 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x1beb7e3 0x1beb668 0x12ffc 0x25fd 0x2525)

libc++abi.dylib: terminate called throwing an exception


但如果使用了输入log的方式,就可以在崩溃之时显示更多的内容了, 而不需要重新去复现就可以大致对崩溃原因进行定位分析。‘

2013-07-16 18:46:05.958 GONONO[12638:c07] will crash now

2013-07-16 18:46:05.960 GONONO[12638:c07] 1heqin, CRASH: *** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 2]

2013-07-16 18:46:05.962 GONONO[12638:c07] heqin, Stack Trace: (

0 CoreFoundation 0x01c9102e __exceptionPreprocess + 206

1 libobjc.A.dylib 0x010cee7e objc_exception_throw + 44

2 CoreFoundation 0x01c46b44 -[__NSArrayI objectAtIndex:] + 196

3 GONONO 0x00002f69 -[ViewController btnClicked:] + 185

4 libobjc.A.dylib 0x010e2705 -[NSObject performSelector:withObject:withObject:] + 77

5 UIKit 0x000162c0 -[UIApplication sendAction:to:from:forEvent:] + 96

6 UIKit 0x00016258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61

7 UIKit 0x000d7021 -[UIControl sendAction:to:forEvent:] + 66

8 UIKit 0x000d757f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578

9 UIKit 0x000d66e8 -[UIControl touchesEnded:withEvent:] + 546

10 UIKit 0x00045cef -[UIWindow _sendTouchesForEvent:] + 846

11 UIKit 0x00045f02 -[UIWindow sendEvent:] + 273

12 UIKit 0x00023d4a -[UIApplication sendEvent:] + 436

13 UIKit 0x00015698 _UIApplicationHandleEvent + 9874

14 GraphicsServices 0x01becdf9 _PurpleEventCallback + 339

15 GraphicsServices 0x01becad0 PurpleEventCallback + 46

16 CoreFoundation 0x01c06bf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53

17 CoreFoundation 0x01c06962 __CFRunLoopDoSource1 + 146

18 CoreFoundation 0x01c37bb6 __CFRunLoopRun + 2118

19 CoreFoundation 0x01c36f44 CFRunLoopRunSpecific + 276

20 CoreFoundation 0x01c36e1b CFRunLoopRunInMode + 123

21 GraphicsServices 0x01beb7e3 GSEventRunModal + 88

22 GraphicsServices 0x01beb668 GSEventRun + 104

23 UIKit 0x00012ffc UIApplicationMain + 1211

24 GONONO 0x000025fd main + 141

25 GONONO 0x00002525 start + 53

)

2013-07-16 18:46:05.962 GONONO[12638:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 2]'

*** First throw call stack:

(0x1c91012 0x10cee7e 0x1c46b44 0x2f69 0x10e2705 0x162c0 0x16258 0xd7021 0xd757f 0xd66e8 0x45cef 0x45f02 0x23d4a 0x15698 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x1beb7e3 0x1beb668 0x12ffc 0x25fd 0x2525)

libc++abi.dylib: terminate called throwing an exception




分享到:
评论

相关推荐

    Crash异常捕获与处理

    通过【NSSetUncaughtExceptionHandler】机制捕获处理app的异常

    TestNSSetUncaughtExceptionHandler:测试NSSetUncaughtExceptionHandler

    TestNSSetUncaughtExceptionHandler测试NSSetUncaughtExceptionHandler

    iOS捕获异常组件UncaughtExceptionHandler

    添加UncaughtExceptionHandler这个类 iOS SDK提供的函数是NSSetUncaughtExceptionHandler来进行异常处理。但是无法处理内存访问错误、重复释放等错误,因为这些错误发送的SIGNAL。所以需要处理这些SIGNAL

    iOS异常处理

    通过应用程序委托中调用[[AppExceptionHandler shareAppExceptionHandler] start];来开启异常捕获;

    SDStatisticsSDK

    SD统计SDK采集应用程序的崩溃信息,主要分为以下两种场景: NSException异常Unix信号异常捕获NSException异常通过NSSetUncaughtExceptionHandler函数来设置异常处理函数,然后收集异常变量信息捕获信号Mach异常和...

    iOS 捕获程序崩溃日志

    1. 在程序启动时加上一个异常捕获监听,用来处理程序崩溃时的回调动作 代码如下: NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler); 官方文档介绍:Sets the top-level error-handling function where...

Global site tag (gtag.js) - Google Analytics