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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值