I use asserts quite a bit in my code since they are useful in debugging, but the standard behaviour of Cocoa applications is to interrupt processing and logging the assertion failure to the console.. the UI stays up neither crashing, nor bringing up an error dialog and it’s often not obvious what has happened.
What’s the easiest way of either crashing the program (at least then you get a trace) or bringing up an error dialog?
Do I need to supply an assertion handler (that looks very complicated!)? or can I catch exceptions in the run loop are something?
Is there any example code available anywhere on how to best do this?
Thanks for your help.
In answer to the subject of your question: You don’t.
To crash immediately:
abort();To raise an error, simply create the NSError object, complete with description (for custom message, error number, etc.), and ask NSApp to -presentError:
I recommend you go the NSError route. Cocoa gives you a LOT of error handling and even error recovery capabilities. It’s infinitely better to provide an error (and potentially recovery options for the user) than to simply crash. After all, if you know where an error is going to occur (enough to call abort() there) and you know what you’re asserting (enough to user NSAssert), then you know enough about how either to recover automatically or to give options to the user so they can decide what to do.
Check out the Error Handling Programming Guide for details.