聲明:本文檔僅為個人學(xué)習(xí)過程中順手翻譯之作,,方便開發(fā)的同胞借鑒參考,。如有覺得譯的不好不到位的地方,歡迎指正,,將及時做出更正
|
Cup *cup = [[Cup alloc] init]; |
|
@try { |
[cup fill]; |
} |
@catch (NSException *exception) { |
NSLog(@"main: Caught %@: %@", [exception name], [exception reason]); |
} |
@finally { |
[cup release]; |
} |
要捕獲 @try{}
塊中拋出的異常, 我們會使用一個或者多個 @catch{}
塊緊跟在 @try{}
塊后面.
@catch{}
塊 對異常的捕獲應(yīng)該先細(xì)后粗,,即是說先捕獲特定的異常,再使用一些泛些的異常類型,。這樣做的好處是你可以為異常的處理進行分組處理,,就像10-1中那樣。
10-1
@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. |
... |
} |
下面闡述了捕獲異常時通常采用的捕獲順序:
優(yōu)先捕獲特定的需要處理的異常類型.
捕獲一些較泛的異常類型
執(zhí)行一些清理進程,,無論異常是否發(fā)生
要拋出一個異常,,你必須提供一些必要的信息,例如異常的名字以及為什么要拋出這個異常
NSException *exception = [NSException exceptionWithName: @"HotTeaException" |
reason: @"The tea is too hot" |
userInfo: nil]; |
@throw exception; |
重要: 在許多環(huán)境中,,異常的使用都很常見,。例如,你可能會拋出一個異常來提示一個例程無法正常執(zhí)行—例如當(dāng)文件缺失或者數(shù)據(jù)轉(zhuǎn)型錯誤,。在Objective-C 中異常是比較消耗資源的,。對于平常的流程控制,或者簡單的錯誤提示,,你就不應(yīng)該去使用異常,,而是使用方法或者函數(shù)的返回值來提示是否有異常發(fā)生,并在一個錯誤對象中提供相關(guān)錯誤信息,。參見 Error Handling Programming Guide.
@catch{}
塊中, 你可以使用 @throw
指令(不需要提供理由)再次拋出你捕獲到的異常,。這種情況下省去理由會讓你的代碼更具可讀性。
沒人限制你必須拋出 NSException
對象. 你可以拋出任何 Objective-C 的對象來作為異常對象. NSException
類提供了可以在異常處理提供幫助的方法,,但如果愿意你可以實現(xiàn)你自己帶方法,。你還可以繼承 NSException
來實現(xiàn)特定的異常類型,例如文件系統(tǒng)異?;蛘咄ㄐ女惓?。
英文原文:點擊打開鏈接
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; for more details, see Exception
Programming Topics.
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 Mac OS X v10.3
and later because runtime support
for exception handling and synchronization is not present in earlier versions of the software.)
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.”)
You use the @throw
directive
to throw an exception, which is essentially an Objective-C object. You typically use an NSException
object,
but you are not required to.
A @finally{}
block
contains code that must be executed whether an exception is thrown or not.
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]; |
} |
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.
Performs any clean-up processing that must always be performed, whether exceptions were thrown or not.
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; |
Important In many environments, use of exceptions is fairly commonplace. For example, you might throw an exception to signal that a routine could not execute normally—such as when a file is missing or data could not be parsed correctly. Exceptions are resource-intensive in Objective-C. You should not use exceptions for general flow-control, or simply to signify errors. Instead you should use the return value of a method or function to indicate that an error has occurred, and provide information about the problem in an error object. For more information, see Error Handling Programming Guide.
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 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.
|