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:



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?


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!



AppKit has its own high level exception handler on the main thread that catches the exception first. You can subclass NSApplication

and override -reportException:

to be able to do something with it.

However, your exception handler may still be called on other threads.

Link: Post by Tim Wood on macosx-dev back in 1999 .



All Articles