I’m trying to understand exceptions a little more. I’ve installed in my AppDelegate an uncaught exception handler:
// in applicationDidFinishLaunching:
NSSetUncaughtExceptionHandler(&HandleExceptions);
...
void HandleExceptions(NSException *exception) {
// Save application data on crash
[appDelegate saveData];
}
So I’m not entirely sure how this works. Say I access an array out of bounds, which throws an exception. Why does this method not get called? Or say there is an assertion exception thrown after inserting or removing rows from a table – how can I save data before the app crashes in those situations?
In general, when the system throws an exception, how can I have the chance to save data before the app crashes? Or does it not work like that? Is the uncaught exception handler only for exceptions that I throw that are not enwrapped in a @catch or @try?
Perhaps ‘appDelegate’ is nil when your method is called. I just added this to a viewController:
then did this:
and got this: