虽然大家都不愿意看到程序崩溃,但可能崩溃是每个应用必须面对的现实,既然崩溃已经发生,无法阻挡了,那我们就让它崩也崩得淡定点吧。

IOS SDK中提供了一个现成的函数 NSSetUncaughtExceptionHandler 用来做异常处理,但功能非常有限,而引起崩溃的大多数原因如:内存访问错误,重复释放等错误就无能为力了,因为这种错误它抛出的是Signal,所以必须要专门做Signal处理。首先定义一个UncaughtExceptionHandler类,代码如下:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>  @interface UncaughtExceptionHandler : NSObject
{  BOOL dismissed;
}
+(void) InstallUncaughtExceptionHandler;
@end  

//利用 NSSetUncaughtExceptionHandler,当程序异常退出的时候,可以先进行处理,然后做一些自定义的动作,比如下面一段代码,就是网上有人写的,直接在发生异常时给某人发送邮件,</span>
void UncaughtExceptionHandlers (NSException *exception);  #import "UncaughtExceptionHandler.h"
#include <libkern/OSAtomic.h>
#include <execinfo.h>
NSString * const UncaughtExceptionHandlerSignalExceptionName = @"UncaughtExceptionHandlerSignalExceptionName";
NSString * const UncaughtExceptionHandlerSignalKey = @"UncaughtExceptionHandlerSignalKey";
NSString * const UncaughtExceptionHandlerAddressesKey = @"UncaughtExceptionHandlerAddressesKey";
volatile int32_t UncaughtExceptionCount = 0;
const int32_t UncaughtExceptionMaximum = 10;
const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4;
const NSInteger UncaughtExceptionHandlerReportAddressCount = 5;
NSString* getAppInfo()
{  NSString *appInfo = [NSString stringWithFormat:@"App : %@ %@(%@)\nDevice : %@\nOS Version : %@ %@\n",  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"],  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"],  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"],  [UIDevice currentDevice].model,  [UIDevice currentDevice].systemName,  [UIDevice currentDevice].systemVersion];  //                         [UIDevice currentDevice].uniqueIdentifier];  NSLog(@"Crash!!!! %@", appInfo);  return appInfo;
}  void MySignalHandler(int signal)
{  int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);  if (exceptionCount > UncaughtExceptionMaximum)  {  return;  }  if(signal==11)  {//比较坑爹的是 我遇到的一个问题只有iPhone5出现问题 但是我这边测试的没有iPhone5 无法直接log  可能是内存不足 果然 删除几个应用就可以了 所以加了这句  UIAlertView * tip2 = [[UIAlertView alloc]initWithTitle:@"可能原因:key" message:@"内存不足" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];  [tip2 show];  [tip2 release];  }  NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:UncaughtExceptionHandlerSignalKey];  NSArray *callStack = [UncaughtExceptionHandler backtrace];  [userInfo setObject:callStack forKey:UncaughtExceptionHandlerAddressesKey];  [[[[UncaughtExceptionHandler alloc] init] autorelease]  performSelectorOnMainThread:@selector(handleException:)  withObject:  [NSException  exceptionWithName:UncaughtExceptionHandlerSignalExceptionName  reason:  [NSString stringWithFormat:  NSLocalizedString(@"Signal %d was raised.\n"  @"%@", nil),  signal, getAppInfo()]  userInfo:  [NSDictionary  dictionaryWithObject:[NSNumber numberWithInt:signal]  forKey:UncaughtExceptionHandlerSignalKey]]  waitUntilDone:YES];  }  @implementation UncaughtExceptionHandler
+(void) InstallUncaughtExceptionHandler
{  signal(SIGABRT, MySignalHandler);  signal(SIGILL, MySignalHandler);  signal(SIGSEGV, MySignalHandler);  signal(SIGFPE, MySignalHandler);  signal(SIGBUS, MySignalHandler);  signal(SIGPIPE, MySignalHandler);
}
+ (NSArray *)backtrace
{  void* callstack[128];  int frames = backtrace(callstack, 128);  char **strs = backtrace_symbols(callstack, frames);  int i;  NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];  for (  i = UncaughtExceptionHandlerSkipAddressCount;  i < UncaughtExceptionHandlerSkipAddressCount +  UncaughtExceptionHandlerReportAddressCount;  i++)  {  [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];  }  free(strs);  return backtrace;
}
- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{  if (anIndex == 0)  {  dismissed = YES;  }
}
- (void)handleException:(NSException *)exception
{  UIAlertView *alert =  [[[UIAlertView alloc]  initWithTitle:NSLocalizedString(@"Unhandled exception", nil)  message:[NSString stringWithFormat:NSLocalizedString(  @"You can try to continue but the application may be unstable.\n"  @"%@\n%@", nil),  [exception reason],  [[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]]  delegate:self  cancelButtonTitle:NSLocalizedString(@"Quit", nil)  otherButtonTitles:NSLocalizedString(@"Continue", nil), nil]  autorelease];  [alert show];  CFRunLoopRef runLoop = CFRunLoopGetCurrent();  CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);  while (!dismissed)  {  for (NSString *mode in (NSArray *)allModes)  {  CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);  }  }  CFRelease(allModes);  NSSetUncaughtExceptionHandler(NULL);  signal(SIGABRT, SIG_DFL);  signal(SIGILL, SIG_DFL);  signal(SIGSEGV, SIG_DFL);  signal(SIGFPE, SIG_DFL);  signal(SIGBUS, SIG_DFL);  signal(SIGPIPE, SIG_DFL);  if ([[exception name] isEqual:UncaughtExceptionHandlerSignalExceptionName])  {  kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKey] intValue]);  }  else  {  [exception raise];  }
}
void UncaughtExceptionHandlers (NSException *exception) {  NSArray *arr = [exception callStackSymbols];  NSString *reason = [exception reason];  NSString *name = [exception name];  NSString *urlStr = [NSString stringWithFormat:@"mailto://1140454645@qq.com?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]);
}  @end  //然后在delegate文件里面- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数里面
[UncaughtExceptionHandler InstallUncaughtExceptionHandler];  NSSetUncaughtExceptionHandler (&UncaughtExceptionHandlers);  

UncaughtExceptionHandler相关推荐

  1. Java并发-UncaughtExceptionHandler捕获线程异常信息并重新启动线程

    Java并发-UncaughtExceptionHandler捕获线程异常信息并重新启动线程 参考文章: (1)Java并发-UncaughtExceptionHandler捕获线程异常信息并重新启动 ...

  2. android崩溃无日志,Android 收集Crash日志----UncaughtExceptionHandler

    Android应用不可避免地会发生crash,也称之为崩溃,无论你的程序写得多么完美,总是无法完全避免crash的发生,可能是由于Android系统底层的bug,也可能是由于不充分的机型适配或者是糟糕 ...

  3. Android UncaughtExceptionHandler 全局异常监控

    2019独角兽企业重金招聘Python工程师标准>>> 一.全局捕获异常 为了解决这样的问题,我们需要能够及时的捕获异常,但要捕获的地方是在太多,因此,我们需要进行全局性的异常捕获, ...

  4. JAVA多线程之UncaughtExceptionHandler——处理非正常的线程中止

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  5. UncaughtExceptionHandler:java未捕获的异常/错误默认处理方式

    UncaughtExceptionHandler:java未捕获的异常/错误 @(JAVA)[java] java有一部分异常属于运行时异常,这类异常发生时通过会导致程序发生错误,导致退出.如数据下标 ...

  6. 【Android】 -- 使用UncaughtExceptionHandler捕捉全局异常

    在综合统计SDK(欧盟统计局的朋友,百度统计)之后.有一个非常有利的功能测试:错误分析.此功能可以在程序的执行中遇到崩溃(runtimeException)反馈给server,帮助开发者提高产品.多功 ...

  7. Android之用UncaughtExceptionHandler实现保存崩溃日志到sdcard目录下的文件夹

    1.异常和UncaughtExceptionHandler的介绍 1).Java异常处理机制中: 如果抛出的是Exception异常的话,需要有try catch进行处理,属于可以捕获exceptio ...

  8. IOS应用之一--异常处理(UncaughtExceptionHandler)

    使用原因 iOS开发中我们会遇到程序抛出异常退出的情况,如果是在调试的过程中,异常的信息是一目了然,但是如果是在已经发布的程序中,获取异常的信息有时候是比较困难的. 好处与缺点 iOS提供了异常发生的 ...

  9. 对线程中未捕获的异常进行处理UncaughtExceptionHandler

    通常程序中我们会对可能出现的异常进行捕获,例如 public static void main(String[] args) {System.out.println("##### begin ...

最新文章

  1. 报告解读 | 智能技术“核聚变”催生智能经济,将拉动十万亿市场
  2. Linux命令之乐--nmap
  3. java 初始化duration_java11教程--类Duration用法
  4. nginx lua调用redis和mongo
  5. Android Intent机制详解
  6. How can I generate database tables from C# classes?
  7. 计算机二级选择题考word基础知识吗,计算机二级ms office只考选择题吗?好考吗
  8. SUN:开源在Web2.0时代
  9. 《编译原理》学习笔记 ·001【第一章:总论】
  10. Javascript特效:图片切换
  11. dota2自走棋 服务器没响应,dota2自走棋国服务器
  12. android 实现果冻动画效果,利用css3实现弹性果冻按钮动画特效
  13. JAVA毕业设计酒店管理系统设计与实现计算机源码+lw文档+系统+调试部署+数据库
  14. REVIT建模如何 一键生成局部三维视图
  15. EF中的上下文(DbContext)简介
  16. nagios二次开发(四)---nagios监控原理和nagios架构简介
  17. python bar函数循环_python bar函数怎么使用
  18. regsvr32 注册.dll的用法
  19. 数据结构实验PTA 7-10 旅游规划 分数 25
  20. HQY的水题大赛解析

热门文章

  1. Mybatis 源码解析 -- 基于配置的源码解析(二)
  2. Bootstrap导航栏
  3. java.lang.System
  4. DRBD编译安装中出现的问题及解决小结
  5. 数据库分库分表(sharding)系列(五) 一种支持自由规划无须数据迁移和修改路由代码的Sharding扩容方案...
  6. 零基础学习hadoop到上手工作线路指导(中级篇)
  7. 精彩十年(2)——阴沟里也翻船
  8. 用Telnet 来用smtp发邮件。。 send mail by SMTP server
  9. django设置paypal支付如何获取signature
  10. ubuntu19.10锁屏时间调节