Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Exception Handling

Objective-C provides support for exception handling and thread synchronization, which are explained in this article and “Threading.” To turn on support for these features, use the -fobjc-exceptions switch of the GNU Compiler Collection (GCC) version 3.3 and later.

Note: Using either of these features in a program, renders the application runnable only in Mac OS X v10.3 and later because runtime support for exception handling and synchronization is not present in earlier versions of the software.

Handling Exceptions

The Objective-C language has an exception-handling syntax similar to that of Java and C++. Coupled with the use of the NSException, NSError, or custom classes, you can add robust error-handling to your programs.

The exception support revolves around four compiler directives: @try, @catch, @throw, and @finally. Code that can potentially throw an exception is enclosed in a @try block. @catch()blocks contain the exception-handling logic for exceptions thrown in a @tryblock. A @finallyblock contains code that must be executed whether an exception is thrown or not. You use the @throwdirective to throw an exception, which is essentially a pointer to an Objective-C object. You can use NSException objects but are not limited to them.

The example below depicts a simple exception-handling algorithm:

Cup *cup = [[Cup alloc] init];
 
@try {
    [cup fill];
}
@catch (NSException *exception) {
    NSLog(@"main: Caught %@: %@", [exception name], [exception  reason]);
}
@finally {
    [cup release];
}

Throwing Exceptions

To throw an exception you must instantiate an object with the appropriate information, such as the exception name and the reason it was thrown.

NSException *exception = [NSException exceptionWithName:@"HotTeaException"
                            reason:@"The tea is too hot"  userInfo:nil];
@throw exception;

Inside a @catch() block, you can re-throw the caught exception using the @throw directive without an argument. This can help make your code more readable.

You can subclass NSException to implement specialized types of exceptions, such as file-system exceptions or communications exceptions.

Note: You are not limited to throwing NSException objects. You can throw any Objective-C object as an exception object. The NSException class provides methods that help in exception processing, but you can implement your own if you so desire.

Processing Exceptions

To catch an exception thrown in a @try block, use one or more @catch()blocks following the @try block. The @catch() blocks should be ordered from most-specific to the least-specific. That way you can tailor the processing of exceptions as groups, as shown in Listing 9-1.

Listing 9-1  An exception handler

@try {
    ...
}
@catch (CustomException *ce) {  // 1
    ...
}
@catch (NSException *ne) {  // 2
    // Perform processing necessary at this level.
    ...
 
    // Rethrow the exception so that it's handled at a higher level.
    @throw;  // 3
}
@catch (id ue) {
    ...
}
@finally {  // 4
    // Perform processing necessary whether an exception occurred  or not.
    ...
}

The following list describes the numbered code-lines:

  1. Catches the most specific exception type.

  2. Catches a more general exception type.

  3. Re-throws the exception caught.

    To compartmentalize exception processing, you can nest exception handlers in a program. That way if a method or function catches an exception that it cannot process, it can re-throw it to the next exception handler.

  4. Performs any clean-up processing that must always be performed, whether exceptions were thrown or not.



< Previous PageNext Page > Hide TOC


Last updated: 2008-06-09




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice