iOS[bug篇]异常处理

2017-03-24  本文已影响270人  一只小蝉儿

其实控制台输出的日志信息就是NSException产生的,一旦程序抛出异常,程序就会崩溃,控制台就会有这些崩溃日志。如下图:


img.png

1、NSException

简单使用:

NSArray*array=@[];
if (array.count<6) {
     //异常的名称
     NSString *exceptionName = @"自定义异常";
     //异常的原因
     NSString *exceptionReason = @"数组溢出";
     //异常的信息
     NSDictionary *exceptionUserInfo = nil;
     NSException *exception = [NSException exceptionWithName:exceptionName reason:exceptionReason userInfo:exceptionUserInfo];
     //抛异常
     @throw exception;
}

当然,我们可以通过类名,行,方法名定位异常的位置:
NSDictionary *dict = @{@"类名": [NSString stringWithFormat:@"%@",[self class]],@"哪一行":[NSString stringWithFormat:@"%ld",(long)__LINE__],@"方法名":[NSString stringWithFormat:@"%s",__PRETTY_FUNCTION__]};

2、 @try{} @catch{}@finally{}代码块

有时候我们希望出现异常的代码段不影响后续代码的执行,这时我们就可以使用如下的代码块:

@try{
   代码块1(可能出现异常的语句)
   //执行的代码,其中可能有异常。一旦发现异常,则立即跳到catch执行。否则不会执行catch里面的内容
} @catch(Exception e){ //catch是抓取代码块1中的异常
   代码块2(发生异常时进行处理)
   //除非try里面执行代码发生了异常,否则这里的代码不会执行
}@finally{
   代码块3(始终要进行处理的语句)
   //不管什么情况都会执行,包括try catch 里面用了return ,可以理解为只要执行了try或者catch,就一定会执行finally
}
代码块4(正常代码流)
//不管什么情况都会执行,除非前面的代码块中执行了return

3、崩溃统计分析

有时候我们需要把崩溃信息统计出来以供分析,这时我们就可以将崩溃信息存到沙盒,等下次程序启动的时候传给服务器(因为传给服务器是个耗时的过程),可以这样做:

在main.m文件中
int main(int argc, char * argv[]) {
    @try{
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    } @catch (NSException *exception) {
        NSLog(@"main----------%@",exception);
        //存进沙盒
        [exception.userInfo writeToFile:<#(nonnull NSString *)#> atomically:<#(BOOL)#> ]
    }
}

测试上面代码可以发现,我们只有在异常抛出的时候才能捕捉它,存进沙盒,如果我们想要在异常抛出之前捕捉到呢?可以这样做:

在AppDelegate.m文件中:
/*
 *拦截异常
 */
void handleException(NSException *exception)
{
    NSMutableDictionary *info =[NSMutableDictionary dictionary];
    info[@"callStack"]=[exception callStackSymbols];//调用栈信息(错误来源于哪个方法)
    info[@"name"] =  [exception name];//异常名字
    info[@"reason"] =[exception reason];//异常描述(报错理由)
    
   //[info writeToFile:<#(nonnull NSString *)#> atomically:<#(BOOL)#>]
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //设置捕捉异常的回调
    NSSetUncaughtExceptionHandler(handleException);
}

另外,有几个程序异常统计的第三方:
友盟、Flurry(国外)、Crashlytics(国外)

4、程序闪退处理

有时候我们需要在程序闪退之前提示下用户,怎么做呢?

//首先我们要拦截异常
void handleException(NSException *exception)
{
    [[UIApplication sharedApplication].delegate performSelector:@selector(handle)];
}
-(void)handle{
    UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"系统出错!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"关闭", nil];
    [alert show];//此时弹框不会显示,因为程序已经死掉,runLoop已经停止
    
    //重新启动RunLoop, 弹框显示
    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //点击关闭按钮,退出程序
    exit(0);// 退出应用程序
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSSetUncaughtExceptionHandler(handleException);
    
    return YES;
}

运行结果:


3月-24-2017 13-44-43.gif

番外:

 Q:1.我们可以使用热修复啊?
    2.服务返回数据的问题,应该是服务的责任吧?
    3.加判断的话全部代码得有多少If啊?而且在多线程的情况下,加判断也未必好使!
    4.如果感觉这段代码要出问题的话,为什么不好好审视重新写呢?
 A:首先肯定你的想法。但有一点你有没有意识到,若发布版本的崩溃造成的影响有多严重!你的想法我可以理解,的确服务器占很大部分的责任。但移动端做送错处理是一个很好的习惯。
上一篇下一篇

猜你喜欢

热点阅读