美文网首页
捕获异常

捕获异常

作者: 三岁就很乖 | 来源:发表于2016-05-09 15:37 被阅读61次

#include <libkern/OSAtomic.h>
#include <execinfo.h>


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    /*
     注意::::::!!!!!
     
     iOS SDK提供的函数是NSSetUncaughtExceptionHandler来进行异常处理。但是无法处理内存访问错误、重复释放等错误,因为这些错误发送的SIGNAL。所以需要处理这些SIGNAL
     */
    
    NSSetUncaughtExceptionHandler(&HandleException);
    
    return YES;
}
void HandleException(NSException *exception)
{
    
    NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息
    NSString *reason = [exception reason];//非常重要,就是崩溃的原因
    NSString *name = [exception name];//异常类型
    NSString *excpCnt = [NSString stringWithFormat:@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr];
    NSLog(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
    
    //保存日志
    NSArray *dieArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dirPath = dieArr[0];
    NSString *logDir = [dirPath stringByAppendingString:@"/CrashLog"];
    
    BOOL isExistLogDir =  YES;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:logDir]) {
        isExistLogDir = [fileManager createDirectoryAtPath:logDir withIntermediateDirectories:NO attributes:nil error:nil];
    }
    if (isExistLogDir) {
        NSString *logPath = [logDir stringByAppendingString:@"/crashLog.txt"];
        [excpCnt writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    
    //第二种方法,询问用户是否发送邮件给开发者
    NSString *urlStr = [NSString stringWithFormat:@"mailto://tianranwuwai@yeah.net?subject=bug报告%错误详情:%@",excpCnt];
    NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:url];

}




相关文章

  • Python面向对象2

    异常 捕获异常image.png根据错误类型捕获异常image.png 未知错误捕获image.png 捕获异常完...

  • 工作总结-002

    异常捕获service捕获业务异常,自定义BaseException;interface捕获业务异常,以及其他异常...

  • C++学习第20课,异常

    1 异常 一句话概括:捕获异常 1.1 谁捕获异常?捕获谁? A捕获B A() { try{ B(); } ...

  • Python 面向对象 - 08 异常

    目录一、概念二、捕获异常2.1 简单的捕获异常语法2.2 错误类型捕获2.3 异常捕获完整语法三、异常的传递四、抛...

  • springboot 异常捕获和处理

    springboot 异常捕获和处理 异常捕获处理

  • iOS 异常捕获处理机制(初级篇)

    一、异常处理简介 二、异常捕获案例 使用@try catch捕获异常 例1是最简单的一种写法: 捕获异常之嵌套捕获...

  • python3.7异常小记

    首先上示例代码: 这段代码包含了,异常分支类型捕获,无异常捕获,有无异常都会捕获的执行逻辑。 捕获全局异常 对入口...

  • Python异常处理

    Python异常处理 一、捕获异常 1.异常捕获语法 2.错误类型捕获 except:处理所有异常。except ...

  • 自定义异常捕获 与 自定返回json 数据格式

    1. 自定义异常捕获 自定的异常 2 捕获异常 =================================...

  • python知识点

    异常捕获 try ... except 捕获所有异常 Exception as reselt 自己定义异常: ra...

网友评论

      本文标题:捕获异常

      本文链接:https://www.haomeiwen.com/subject/nujerttx.html