13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4: <strong>Objective</strong>-C Language Features 81}}{}@autoreleasepool{//some other operation that creates a lot of temporary objects}Any object marked for autorelease in the inner loop is released at the end of each inner loopiteration, as you expect.ExceptionsExceptions in <strong>Objective</strong>-C are just like exceptions in any other language that supports them.What makes exceptions in <strong>Objective</strong>-C unusual is how and when they are used.Exceptions, encapsulated in an NSException object, represent truly exceptional circumstances. Ifit is possible to foresee the circumstances that would cause an exception, don’t use exceptions.Exceptions are reserved for serious problems, such as a disk becoming disconnected during awrite operation, an accidental division by zero, or accessing an out-of-bounds array element.Most exceptions encountered during development are due to developer error. In fact, that’s whatApple says they should be used for: to notify developers that they’re doing something wrong.Nevertheless, no book on <strong>Objective</strong>-C would be complete without at least a summary of howexceptions work.Throwing exceptions is accomplished by instantiating an NSException objecting and then callingits raise method. Alternatively, NSException defines a convenience class method for raisingexceptions. The two methods are demonstrated in Listing 4-5.Listing 4-5. NSException ExamplesNSException * exception = [NSExceptionexceptionWithName:@"DiskBecameUnavilableDuringWrite"reason:@"Disk Became Unavailable During a Write Operation"userInfo:nil];[exception raise];[NSException raise: @"DiskBecameUnavilableDuringWrite"format:@"Disk Became Unavailable During a Write Operation"];The latter example uses a format string for its second parameter. Also note that the formerexample specifies a userInfo parameter. This parameter is an NSDictionary instance that can beused to provide details about the exception.NSException.h provides several built-in exception names that can be used by any developer.They are listed in Table 4-1.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!