未调用的异常处理程序 [英] Uncaught exception handler not called
问题描述
我尝试在我的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屋!