久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Objective-C 編程語言官網(wǎng)文檔(十一)-異常的處理

 現(xiàn)在決定明天 2015-10-20

聲明:本文檔僅為個人學(xué)習(xí)過程中順手翻譯之作,,方便開發(fā)的同胞借鑒參考,。如有覺得譯的不好不到位的地方,歡迎指正,,將及時做出更正

盡量尊重原文檔,,因為首次Objective-C,有些地方可能直譯了沒有注意該語言的專有詞,希望指正,。如需轉(zhuǎn)載,,請注明出處


我的編程環(huán)境:

IDE:XCODE4.3.1

OS: MAC OS X 10.7.4

文章來譯自:http://developer.apple.com/


異常的處理

Objective-C 的異常處理語法與Java和C++類似。通過使用NSExceptionNSError以及自定義的異常處理類,,你可以為你的程序添加健壯的異常處理系統(tǒng),。本節(jié)提供了有關(guān)異常語法以及處理的一些概要信息。更多詳情可以參考 Exception Programming Topics.

激活異常處理

如果使用的是 GNU Compiler Collection (GCC) 3.3 以及更新的版本的話, Objective-C 提供了語言級別的異常處理支持,。要打開對這些特性的支持,,可以使用GCC的 -fobjc-exceptions 開關(guān)。(要注意這個開關(guān)只有在 Mac OS X v10.3 以及更新的版本中才能使用,,因為對異常處理以及同步的運行時支持在早前的版本中還沒有出現(xiàn))

異常處理

異常就是當(dāng)程序執(zhí)行是發(fā)生的某種特殊狀況,,打斷了正常的運轉(zhuǎn)流程。硬件或者軟件,,發(fā)生異常的原因很多(異常通常被說成是raised 或者 thrown),。例如,,一個數(shù)學(xué)計算錯誤,一個數(shù)被0所除,,下溢或者溢出, 調(diào)用未定義的指令 (例如試圖調(diào)用一個為實現(xiàn)的方法), 以及試圖訪問一個集合時下標(biāo)越界,。

Objective-C 對異常的支持包括四個編譯器指令: @try@catch@throw 以及 @finally:

  • 代碼中可能拋出異常時使用 @try{} 塊.

  •  @catch{} 塊包含異常的處理邏輯,以處理 @try{} 塊中拋出的異常. 你可以有多個 @catch{} 塊來處理不同的異常,。 (如果像看一個代碼實例的話,,可以參見 “Catching Different Types of Exception.”)

  • 可以使用 @throw 指令來拋出一個異常,本質(zhì)上來說這是一個 Objective-C 對象. 通常你會用到一個 NSException 對象,,但不是必須的,。

  • @finally{} 塊包含有無論是否會拋出異常都要執(zhí)行的代碼。(即無論是否會拋出異常,,這個塊中的代碼都會得到執(zhí)行)

下面是一個處理異常的典型結(jié)構(gòu)示例

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

下面闡述了捕獲異常時通常采用的捕獲順序:

  1. 優(yōu)先捕獲特定的需要處理的異常類型.

  2. 捕獲一些較泛的異常類型

  3. 執(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)異?;蛘咄ㄐ女惓?。

英文原文:點擊打開鏈接

Exception Handling

The Objective-C language has an exception-handling syntax similar to that of Java and C++. By using this syntax with the NSExceptionNSError, 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.

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

  • @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.

  • @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];
}

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:

  1. Catches the most specific exception type.

  2. Catches a more general exception type.

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

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;

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.


    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多