« Custom Delegates in CocoaXCode Tip #1: Quick Documentation Lookup »

Exceptions in Cocoa

01/03/07

Permalink 11:04:38 pm, by rekle Email , 144 words   English (US)
Categories: Cocoa

Exceptions in Cocoa

Link: http://www.cocoadev.com/index.pl?ExceptionHandling

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.

No feedback yet

February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        
Copyright © 2005 - 2011, Rick Ekle

Comments? Contact me at rick@ekle.us or visit me on Twitter at @rekle

Search

XML Feeds

User tools

powered by b2evolution