Exception Handling
The Objective-C language has an exception-handling syntax similar to that of Java and C++. By using this syntax with the
NSException
, NSError
, or custom classes, you can add robust error-handling to your programs. This chapter provides a summary of exception syntax and handling; Enabling Exception-Handling
Using GNU Compiler Collection (GCC) version 3.3 and later, Objective-C provides language-level support for exception handling. To turn on support for these features, use the
-fobjc-exceptions
switch of the GNU Compiler Collection (GCC) version 3.3 and later. (Note that this switch renders the application runnable only in OS X v10.3 and later because runtime support for exception handling and synchronization is not present in earlier versions of the software.)Exception Handling
An exception is a special condition that interrupts the normal flow of program execution. There are a variety of reasons why an exception may be generated (exceptions are typically said to be raised or thrown), by hardware as well as software. Examples include arithmetical errors such as division by zero, underflow or overflow, calling undefined instructions (such as attempting to invoke an unimplemented method), and attempting to access a collection element out of bounds.
Objective-C exception support involves four compiler directives:
@try
, @catch
, @throw
, and @finally
:- Code that can potentially throw an exception is enclosed in a
@try{}
block. - A
@catch{}
block contains exception-handling logic for exceptions thrown in a@try{}
block. You can have multiple@catch{}
blocks to catch different types of exception. (For a code example, see “Catching Different Types of Exception.”)
This example 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];
|
}
|
Catching Different Types of Exception
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 least-specific. That way you can tailor the processing of exceptions as groups, as shown in Listing 10-1.
Listing 10-1 An exception handler
@try {
|
...
|
}
|
@catch (CustomException *ce) { // 1
|
...
|
}
|
@catch (NSException *ne) { // 2
|
// Perform processing necessary at this level.
|
...
|
|
}
|
@catch (id ue) {
|
...
|
}
|
@finally { // 3
|
// Perform processing necessary whether an exception occurred or not.
|
...
|
}
|
The following list describes the numbered code lines:
- Catches the most specific exception type.
- Catches a more general exception type.
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 rethrow the caught exception using the @throw
directive without providing an argument. Leaving out the argument in this case can help make your code more readable.
You are not limited to throwing
Courtesy:http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Exceptions/Tasks/HandlingExceptions.html#//apple_ref/doc/uid/20000059-SW1
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. You can also subclass NSException
to implement specialized types of exceptions, such as file-system exceptions or communications exceptions.Courtesy:http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/Exceptions/Tasks/HandlingExceptions.html#//apple_ref/doc/uid/20000059-SW1
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
comments