01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Summary<br />

211<br />

10.3.2 Catching exceptions<br />

Code that you expect to potentially throw an exception should be wrapped in a @try<br />

block, followed by one or more @catch() blocks that will catch exceptions that might<br />

get thrown or raised by the code in the @try block. Optionally, you can add a<br />

@finally block. The code in a @finally block gets run regardless of whether or not<br />

an exception occurred. Why can there be multiple @catch blocks? Because you can<br />

catch different kinds of exceptions, and the <strong>Objective</strong>-C runtime will pick the bestmatching<br />

@catch block for the exception that’s been thrown. Note that each @try,<br />

@catch, and @finally block has its own scope, so you can’t access an object in the<br />

@finally block that’s been declared in the @try block, for example. If you want to<br />

accomplish that, you must declare the variable outside of the @try block, in its enclosing<br />

scope. The following listing shows a simple example.<br />

Listing 10.7<br />

Catching an exception<br />

@try {<br />

NSLog(@"Trying to add nil to an array...");<br />

NSMutableArray *array = [NSMutableArray array];<br />

[array addObject:nil];<br />

}<br />

@catch (NSException *e) {<br />

NSLog(@"Caught exception: %@, Reason: %@", [e name], [e reason]);<br />

}<br />

@finally {<br />

NSLog(@"Executed whether an exception was thrown or not.");<br />

}<br />

Inside the @try block, you attempt to insert nil into an array. The exception is caught<br />

by the @catch block, and afterwards the @finally block is run. The console output of<br />

this code looks like this:<br />

Trying to add nil to an array...<br />

Caught exception: NSInvalidArgumentException, Reason: *** -<br />

[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0<br />

Executed whether or not an exception was thrown.<br />

These exception-handling fundamentals should get you far, especially because exceptions<br />

are rarely used in Cocoa development—but they’re useful for catching programming<br />

errors.<br />

10.4 Summary<br />

Error handling in Cocoa and Cocoa Touch is both simple and flexible. Don’t ignore<br />

errors in your applications: handle them gracefully and tell your users about errors<br />

they need to know about in an understandable way. The concept of exceptions is present<br />

in Cocoa as well but plays a minor role compared to other frameworks and languages.<br />

Use them as intended: to catch errors during development.<br />

In chapter 11, we dive into an important Cocoa concept that lets you access and<br />

manipulate values in powerful ways: Key-Value Coding and NSPredicate.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!