一群程序猿的秘密基地iOS知识积累iOS Developer

异常处理、程序的崩溃路径捕捉

2016-05-25  本文已影响3815人  i赵磊

控制面板的输出:

`2016-05-22 17:43:38.972 Exception[1242:39949] -[__NSArray0 addObject:]: unrecognized selector sent to instance 0x7fe8115024f0
2016-05-22 17:43:43.535 Exception[1242:39949] 名字:NSInvalidArgumentException
2016-05-22 17:43:43.535 Exception[1242:39949] 原因:-[__NSArray0 addObject:]: unrecognized selector sent to instance 0x7fe8115024f0
2016-05-22 17:43:43.535 Exception[1242:39949] 用户信息:(null)
2016-05-22 17:43:43.536 Exception[1242:39949] 栈内存地址:(0x105420e4d 0x104e99deb 0x10542948d 0x10537690a 0x1053764b8 0x10499b44c 0x105963f98 0x1059642e7 0x10583aab0 0x10583b199 0x10584cc2e 0x1057c5663 0x1057cbcc6 0x1057c8e7b 0x108199754 0x108199ac2 0x10534ca31 0x10534295c 0x105341e13 0x105341828 0x1057c87cd 0x1057cd610 0x10499bacf 0x107b5c92d)
2016-05-22 17:43:43.536 Exception[1242:39949] 栈描述:(
    0   CoreFoundation                      0x0000000105420e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104e99deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010542948d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010537690a ___forwarding___ + 970
    4   CoreFoundation                      0x00000001053764b8 _CF_forwarding_prep_0 + 120
    5   Exception                           0x000000010499b44c -[ViewController viewDidLoad] + 156
    6   UIKit                               0x0000000105963f98 -[UIViewController loadViewIfRequired] + 1198
    7   UIKit                               0x00000001059642e7 -[UIViewController view] + 27
    8   UIKit                               0x000000010583aab0 -[UIWindow addRootViewControllerViewIfPossible] + 61
    9   UIKit                               0x000000010583b199 -[UIWindow _setHidden:forced:] + 282
    10  UIKit                               0x000000010584cc2e -[UIWindow makeKeyAndVisible] + 42
    11  UIKit                               0x00000001057c5663 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
    12  UIKit                               0x00000001057cbcc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760
    13  UIKit                               0x00000001057c8e7b -[UIApplication workspaceDidEndTransaction:] + 188
    14  FrontBoardServices                  0x0000000108199754 -[FBSSerialQueue _performNext] + 192
    15  FrontBoardServices                  0x0000000108199ac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    16  CoreFoundation                      0x000000010534ca31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x000000010534295c __CFRunLoopDoSources0 + 556
    18  CoreFoundation                      0x0000000105341e13 __CFRunLoopRun + 867
    19  CoreFoundation                      0x0000000105341828 CFRunLoopRunSpecific + 488
    20  UIKit                               0x00000001057c87cd -[UIApplication _run] + 402
    21  UIKit                               0x00000001057cd610 UIApplicationMain + 171
    22  Exception                           0x000000010499bacf main + 111
    23  libdyld.dylib                       0x0000000107b5c92d start + 1
)
2016-05-22 17:43:43.582 Exception[1242:39949] 系统版本1.0
2016-05-22 17:43:43.582 Exception[1242:39949] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArray0 addObject:]: unrecognized selector sent to instance 0x7fe8115024f0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105420e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104e99deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010542948d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010537690a ___forwarding___ + 970
    4   CoreFoundation                      0x00000001053764b8 _CF_forwarding_prep_0 + 120
    5   Exception                           0x000000010499b44c -[ViewController viewDidLoad] + 156
    6   UIKit                               0x0000000105963f98 -[UIViewController loadViewIfRequired] + 1198
    7   UIKit                               0x00000001059642e7 -[UIViewController view] + 27
    8   UIKit                               0x000000010583aab0 -[UIWindow addRootViewControllerViewIfPossible] + 61
    9   UIKit                               0x000000010583b199 -[UIWindow _setHidden:forced:] + 282
    10  UIKit                               0x000000010584cc2e -[UIWindow makeKeyAndVisible] + 42
    11  UIKit                               0x00000001057c5663 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
    12  UIKit                               0x00000001057cbcc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760
    13  UIKit                               0x00000001057c8e7b -[UIApplication workspaceDidEndTransaction:] + 188
    14  FrontBoardServices                  0x0000000108199754 -[FBSSerialQueue _performNext] + 192
    15  FrontBoardServices                  0x0000000108199ac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    16  CoreFoundation                      0x000000010534ca31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x000000010534295c __CFRunLoopDoSources0 + 556
    18  CoreFoundation                      0x0000000105341e13 __CFRunLoopRun + 867
    19  CoreFoundation                      0x0000000105341828 CFRunLoopRunSpecific + 488
    20  UIKit                               0x00000001057c87cd -[UIApplication _run] + 402
    21  UIKit                               0x00000001057cd610 UIApplicationMain + 171
    22  Exception                           0x000000010499bacf main + 111
    23  libdyld.dylib                       0x0000000107b5c92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
崩溃提示:
屏幕快照 2016-05-22 下午5.46.26.png

捕捉崩溃路径方法的实现及介绍

AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //异常对象:NSException
    //参数:将函数的地址给它
    NSSetUncaughtExceptionHandler(&getException);
    return YES;
}
//获得异常的C函数
void getException(NSException *exception)
{
    NSLog(@"名字:%@",exception.name);
    NSLog(@"原因:%@",exception.reason);
    NSLog(@"用户信息:%@",exception.userInfo);
    NSLog(@"栈内存地址:%@",exception.callStackReturnAddresses);
    NSLog(@"栈描述:%@",exception.callStackSymbols);
    //每次启动的时候将,捕获的异常信息,反馈给服务器
    //获取当前设备
    UIDevice*divice=[UIDevice currentDevice];
    //1.系统版本
    NSString*systemVersion=divice.systemVersion;
    //2.app当前版本
    //先获取当前infoplist文件数据
    NSDictionary*infoDic=[[NSBundle mainBundle] infoDictionary];
    //然后从字典中取出版本号
    NSString*version=[infoDic objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"系统版本%@",version);
    //3.系统时间
    NSDate*date=[NSDate date];
    //4.设备种类
    NSString*model=divice.model;
    //将捕获的异常数据进行保存,保存到本地
    //可以在下一次启动的时候将数据发给服务器
}

本次导致崩溃的地方

ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray*arr=[[NSArray alloc]init];
    NSMutableArray*mArr=(NSMutableArray*)arr;
    [mArr addObject:@“1”];//**因为这里是对不可变数组进行增加元素,所以会产生崩溃
}
@end


上一篇下一篇

猜你喜欢

热点阅读