P. 1
ObjC

ObjC

|Views: 5|Likes:
Published by onurertin

More info:

Published by: onurertin on Sep 14, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/31/2011

pdf

text

original

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 10-1.

Listing 10-1 An exception handler

@try {

...

}

// 1

@catch (CustomException *ce) {
...

}

// 2

@catch (NSException *ne) {
// Perform processing necessary at this level.
...

// Rethrow the exception so that it's handled at a higher level.

// 3

@throw;

}
@catch (id ue) {
...

}

// 4

@finally {

// 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.

96

Handling Exceptions

2008-11-19 | © 2008 Apple Inc. All Rights Reserved.

C H A P T E R 1 0

Exception Handling

3. Re-throws the exception caught.

To compartmentalize exception processing, you can nest exception handlers in a program. That way if
amethodorfunctioncatchesanexceptionthatitcannotprocess,itcanre-throwittothenextexception
handler.

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

not.

Handling Exceptions

97

2008-11-19 | © 2008 Apple Inc. All Rights Reserved.

C H A P T E R 1 0

Exception Handling

98

Handling Exceptions

2008-11-19 | © 2008 Apple Inc. All Rights Reserved.

C H A P T E R 1 0

Exception Handling

Objective-Cprovidessupportforthreadsynchronizationandexceptionhandling,whichareexplainedinthis
article and “Exception Handling” (page 95). 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.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->