未调用的异常处理程序 [英] Uncaught exception handler not called

查看:122
本文介绍了未调用的异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的Mac应用程序捕获异常,以便我可以将它们记录在自定义日志文件中。我这样实现异常处理程序:

  void uncaughtExceptionHandler(NSException * exception){
NSLog有用!);
}

我将其设置在 -applicationDidFinishLaunching :方法如下:

  NSSetUncaughtExceptionHandler(& uncaughtExceptionHandler); 

然后我引起一个异常,像这样测试:

  [[NSArray arrayWithObject:@object] objectAtIndex:1]; 

异常被记录到控制台,但我的异常处理程序未被调用。



任何想法?

解决方案

解决方案是使用 ExceptionHandling 框架。以下是我的功能:



-applicationDidFinishLaunching:

  [[NSExceptionHandler defaultExceptionHandler] setExceptionHandlingMask:NSLogAndHandleEveryExceptionMask]; 
[[NSExceptionHandler defaultExceptionHandler] setDelegate:self];然后在我的App Delegate类中,我实现了两个委托方法,





- (BOOL)exceptionHandler:(NSExceptionHandler *)sender shouldLogException:(NSException *)异常掩码:(NSUInteger)aMask
- (BOOL)exceptionHandler: (NSExceptionHandler *)sender shouldHandleException:(NSException *)exception mask:(NSUInteger)aMask

捕获所有异常!


I'm trying to catch exceptions on my Mac app so that I can log them in a custom log file. I'm implementing the exception handler like this:

void uncaughtExceptionHandler(NSException *exception) {
    NSLog(@"It Works!");
}

And I'm setting it in my -applicationDidFinishLaunching: method like this:

NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

Then I cause an exception to test it like this:

[[NSArray arrayWithObject:@"object"] objectAtIndex:1];

The exception gets logged to the console, but my exception handler is not being called.

Any ideas?

解决方案

The solution is to use the ExceptionHandling framework. Here's how I did it:

In -applicationDidFinishLaunching:

[[NSExceptionHandler defaultExceptionHandler] setExceptionHandlingMask:NSLogAndHandleEveryExceptionMask];
[[NSExceptionHandler defaultExceptionHandler] setDelegate:self];

Then in my App Delegate class I implement to two delegate methods,

- (BOOL)exceptionHandler:(NSExceptionHandler *)sender shouldLogException:(NSException *)exception mask:(NSUInteger)aMask
- (BOOL)exceptionHandler:(NSExceptionHandler *)sender shouldHandleException:(NSException *)exception mask:(NSUInteger)aMask

Now I can catch all exceptions!

这篇关于未调用的异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆