App Will Not Terminate After Uncaught Excpetion
App Will Not Terminate After Uncaught Excpetion
- Subject: App Will Not Terminate After Uncaught Excpetion
- From: Andreas Grosam <email@hidden>
- Date: Thu, 16 Dec 2010 14:38:32 +0100
Hi All,
An application (NSApplication) will not terminate if an exception is thrown:, eg:
- (void) applicationDidFinishLaunching:(NSNotification*)notification
{
NSAssert(0, @"failed");
}
or
- (IBAction) buttonPressed:(id) sender
{
[self throwFatalError]; // throws NSException
}
The application will continue to run. Please note, that even after an NSAssert that fails, the app happily keeps going! It looks like, the application's event handler logs a message and then carries on. As a consequence, the "uncaught exception handler" will not be called, since apparently the exception is considered "handled" just by catching it.
In Cocoa, exceptions are considered fatal errors, and code is usually not exception safe. That is, after catching an exception, it is very probable that the application state is corrupted and can not be restored. So, is continuing the app after an uncaught exception occurred expected behavior? Shouldn't the default behavior in Cocoa cause the app to terminate as soon as possible?
NSApplicationDelegate does not have methods to deal with (so far) uncaught exceptions. And even if, we couldn't generally recover from exceptions anyway. However, a delegate method would be useful where the app signals within its event handler that an uncaught exception has been occurred. Then, in the delegate, it would be possible to perform last minute things and terminate - or going on as we desire.
But since there is no such delegate method, how can I change the default behavior?
Do I have to override NSApplication's -run method in order to terminate after an uncaught exception, and if yes, how?
Additional note: I found another proposed solution in stackoverflow: <http://stackoverflow.com/questions/3336278/why-is-raising-an-nsexception-not-bringing-down-my-application> which suggest to add categories to NSApplication which replaces the original method -reportException: and requires a few other methods to be implemented. But this looks pretty awkward to me.
Any other suggestions?
Regards
Andreas
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden