创建一个方法(处理异常并输出到控制台):
1 void uncaughtExceptionHandler(NSException *exception) 2 { 3 NSLog(@"CRASH: %@", exception); 4 NSLog(@"Stack Trace: %@", [exception callStackSymbols]); 5 // Internal error reporting 6 }
在 app delegate 中添加异常处理器(exception handler):
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2 { 3 NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); 4 // Normal launch stuff 5 }
效果如下:
2012-10-27 08:44:29.942 CT[306:fb03] Stack Trace: ( 0 CoreFoundation 0x016cf03e __exceptionPreprocess + 206 1 libobjc.A.dylib 0x01860cd6 objc_exception_throw + 44 2 CoreFoundation 0x01677a48 +[NSException raise:format:arguments:] + 136 3 Foundation 0x009d12cb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 4 UIKit 0x000bf3d7 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 12439 5 UIKit 0x000ca6d2 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 295 6 UIKit 0x000ca711 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 55 7 CT 0x0000ef42 -[CTXAddViewController setEditing:animated:] + 434 8 UIKit 0x0010de4c -[UIViewController(UINavigationControllerItem) setEditing:] + 49 9 CT 0x0000ec1f -[CTXAddViewController viewDidLoad] + 655 10 UIKit 0x00101a1e -[UIViewController view] + 184 11 UIKit 0x00100fec -[UIViewController nextResponder] + 34 12 UIKit 0x00127f1d -[UIResponder _containsResponder:] + 40 13 UIKit 0x001121cb -[UINavigationController defaultFirstResponder] + 83 14 UIKit 0x00128df1 -[UIResponder(Internal) _deepestDefaultFirstResponder] + 36 15 UIKit 0x00128ea9 -[UIResponder(Internal) _promoteDeepestDefaultFirstResponder] + 36 16 UIKit 0x00322508 -[UIWindowController transitionViewDidStart:] + 89 17 UIKit 0x000df401 -[UITransitionView _didStartTransition] + 93 18 UIKit 0x000e008b -[UITransitionView transition:fromView:toView:] + 1169 19 UIKit 0x00321d6c -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 6114 20 UIKit 0x00108857 -[UIViewController presentViewController:withTransition:completion:] + 3579 21 UIKit 0x001089bc -[UIViewController presentViewController:animated:completion:] + 112 22 UIKit 0x001089fc -[UIViewController presentModalViewController:animated:] + 56 23 UIKit 0x00470f4a -[UIStoryboardModalSegue perform] + 250 24 UIKit 0x004654d0 -[UIStoryboardSegueTemplate perform:] + 174 25 CoreFoundation 0x016d0e99 -[NSObject performSelector:withObject:withObject:] + 73 26 UIKit 0x0003d14e -[UIApplication sendAction:to:from:forEvent:] + 96 27 UIKit 0x0027ba0e -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 145 28 CoreFoundation 0x016d0e99 -[NSObject performSelector:withObject:withObject:] + 73 29 UIKit 0x0003d14e -[UIApplication sendAction:to:from:forEvent:] + 96 30 UIKit 0x0003d0e6 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 31 UIKit 0x000e3ade -[UIControl sendAction:to:forEvent:] + 66 32 UIKit 0x000e3fa7 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503 33 UIKit 0x000e3266 -[UIControl touchesEnded:withEvent:] + 549 34 UIKit 0x000623c0 -[UIWindow _sendTouchesForEvent:] + 513 35 UIKit 0x000625e6 -[UIWindow sendEvent:] + 273 36 UIKit 0x00048dc4 -[UIApplication sendEvent:] + 464 37 UIKit 0x0003c634 _UIApplicationHandleEvent + 8196 38 GraphicsServices 0x015b9ef5 PurpleEventCallback + 1274 39 CoreFoundation 0x016a3195 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53 40 CoreFoundation 0x01607ff2 __CFRunLoopDoSource1 + 146 41 CoreFoundation 0x016068da __CFRunLoopRun + 2218 42 CoreFoundation 0x01605d84 CFRunLoopRunSpecific + 212 43 CoreFoundation 0x01605c9b CFRunLoopRunInMode + 123 44 GraphicsServices 0x015b87d8 GSEventRunModal + 190 45 GraphicsServices 0x015b888a GSEventRun + 103 46 UIKit 0x0003a626 UIApplicationMain + 1163 47 CT 0x000020dd main + 141 48 CT 0x00002045 start + 53 )