Take the 2-minute tour ×

I am implementing a Mac App and I want to handle following events:

  • Unhandled Exception
  • Program Crash (memory error dcc)

If I detect them, I can send details to me to analyze and fix bugs using one of the Crash Handlers that I found. Alas I am unable to figure out how to intercept crashes and exceptions.

  1. First question: Have I to differentiate Exceptions from Crashes ? Or detecting Exception is enough?
  2. How can I catch Exceptions and/or crashes redirecting them to my handler ?

PS I tried following in my MyApp class

signal(SIGABRT, SignalHandler);
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);

but it doesn't work. Every time it crashes, it goes to debugger without classing SignalHandler or uncaughtExceptionHandler

share|improve this question
add comment

1 Answer

I have found the best way is to create a simple Exception handling delegate class as this allows exceptions in IBAction methods to be caught.


@interface ExceptionDelegate : NSObject
static ExceptionDelegate *exceptionDelegate = nil; 

int main(int argc, char **argv)
    int retval = 1;

        // Set exception handler delegate
        exceptionDelegate = [[ExceptionDelegate alloc] init];
        NSExceptionHandler *exceptionHandler = [NSExceptionHandler defaultExceptionHandler];
        exceptionHandler.exceptionHandlingMask = NSLogAndHandleEveryExceptionMask;
        exceptionHandler.delegate = exceptionDelegate;

        // Set signal handler
        int signals[] =
        const unsigned numSignals = sizeof(signals) / sizeof(signals[0]);
        struct sigaction sa;
        sa.sa_sigaction = signalHandler;
        sa.sa_flags = SA_SIGINFO;
        for (unsigned i = 0; i < numSignals; i++)
            sigaction(signals[i], &sa, NULL);



    return retval;    

static void signalHandler(int sig, siginfo_t *info, void *context)
    logerr(@"Caught signal %d", sig);

@implementation ExceptionDelegate

- (BOOL)exceptionHandler:(NSExceptionHandler *)exceptionHandler
      shouldLogException:(NSException *)exception
                    mask:(unsigned int)mask
    logerr(@"An unhandled exception occurred: %@", [exception reason]);
   return YES;

- (BOOL)exceptionHandler:(NSExceptionHandler *)exceptionHandler
   shouldHandleException:(NSException *)exception
                    mask:(unsigned int)mask

    // not reached
    return NO;


You'll need to add the ExceptionHandling.framework to your project.

share|improve this answer
Thank you. I add that for testing this you have to disable GDB. If GDB is enabled your signal handler is never called –  pierocampanelli Nov 30 '12 at 7:33
@pierocampanelli I don't think that's true, however I use LLDB. What version of Xcode are you using? –  trojanfoe Nov 30 '12 at 7:38
4.5.2 and i am using GDB. I needed to disable Debugging setting debugger to none. –  pierocampanelli Nov 30 '12 at 13:29
It's probably because gdb is trapping the signals itself. Look at this page in the user guide for ways of stopping it handling signals: sourceware.org/gdb/current/onlinedocs/gdb/Signals.html#Signals –  trojanfoe Nov 30 '12 at 13:33
add comment

Your Answer


