Exceptions in Cocoa
Posted by rekle on Jan 03 2007 in Cocoa
Here's how to do exceptions in Cocoa... It's nearly identical to C++ exceptions:
@try { // Do some code here that might throw an exception. } @catch( NSException* e ) { // Log the exception. NSLog( @"Exception: %@, %@", [e name], [e reason] ); }
One important thing to remember when doing these types of exceptions is that it will only work for apps written for OS X 10.3 (Panther) or later. This shouldn't be a problem, though.
Also, in order to get these to work, you need to turn on Objective-C exceptions in the XCode Build settings. The easiest way to do this is to bring up the Project Info dialog box, click on the 'Build' tab, then use the search box in the upper right to search for 'exception'. The last one should say 'Enable Objective-C Exceptions'. Click that item to put a check box in the box, rebuild and you are done.