Charlie S
Charlie S

Reputation: 4604

try catch around entire app

I was wondering if it is possible to put a try catch around the entire app? My plan was to do this and then present any errors in a UIAlertView with an 'email developer' button that prepopulates an email to me.

Although not a perfect user experience, better than the app simply crashing.

Upvotes: 1

Views: 483

Answers (1)

Avi Tsadok
Avi Tsadok

Reputation: 1843

You can do something nice. 'NSSetUncaughtExceptionHandler'.

in your applicationDidFinishLaunchingWithOptions add:

NSSetUncaughtExceptionHandler(&HandleException);

And of course add the c function to this class:

void HandleException(NSException *exception)
{
        int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);
if (exceptionCount > UncaughtExceptionMaximum)
{
    return;
}

NSArray *callStack = [UncaughtExceptionHandler backtrace];
NSMutableDictionary *userInfo =
    [NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];
[userInfo
    setObject:callStack
    forKey:UncaughtExceptionHandlerAddressesKey];

[[[[UncaughtExceptionHandler alloc] init] autorelease]
    performSelectorOnMainThread:@selector(handleException:)
    withObject:
        [NSException
            exceptionWithName:[exception name]
            reason:[exception reason]
            userInfo:userInfo]
    waitUntilDone:YES];
}

Add the handleException method to display the error whenever you want.

EDIT: You can even see the stack trace: see 'callStackSymbols' of NSException.

Upvotes: 1

Related Questions