Let's say my app does something unrecoverable (this never happens, right? :P ). Should I use exit()? Should I use NSAssert(false, @"Foo")? Should I throw something somehow? Ha开发者_StackOverflow中文版rd answer to google for.
I don't think you should force quit your app if something bad happens as it is annoying for the user - they won't have a clue about what went wrong. Also, Apple are likely to reject your app if this is a common enough occurrence.
Instead you should pop up a dialog to say what went wrong (eg error writing to database, please try again).
If your app can end up in a state where it is totally broken, you should re-design your app. Could you give an example of something "unrecoverable"?
If your app does something really unrecoverable, chances are that it will be terminated by the O.S. without you having the possibility of knowing it. Think of accessing a deallocated object.
If your are considering errors or unforeseen conditions that your program might detect, then it depends very much on what it is about. But a general policy is:
informing the user about the fact that an error happened;
trying to recover as much as possible;
gracefully exit the application.
1 and 3 explicitly rule out both NSAssert
and exit()
options you mention. Throwing does not help either, because if you throw an exception and don't handle it, your program will be terminated abruptly.
Gracefully exiting means exiting like in a normal case, when all dealloc methods are called, all files are closed, all resources are released. And possibly store enough information about the running state of the program so that it becomes possible to recover at the next startup.
精彩评论