Unreturned Exception Handler
I am trying to catch exceptions in my Mac application so that I can log them to a custom log file. I am implementing an exception handler like this:
void uncaughtExceptionHandler(NSException *exception) {
NSLog(@"It Works!");
}
And I am setting it in my method -applicationDidFinishLaunching:
like this:
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
Then I throw an exception to check it like this:
[[NSArray arrayWithObject:@"object"] objectAtIndex:1];
The exception is logged in the console, but my exception handler is not being called.
Any ideas?
+3
source to share
2 answers
The solution is to use a framework ExceptionHandling
. This is how I did it:
IN -applicationDidFinishLaunching:
[[NSExceptionHandler defaultExceptionHandler] setExceptionHandlingMask:NSLogAndHandleEveryExceptionMask];
[[NSExceptionHandler defaultExceptionHandler] setDelegate:self];
Then, in the App Delegate class, I implement 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!
+4
source to share