
- Objective-C基礎
- Objective-C - 首頁
- Objective-C - 概述
- Objective-C - 環境搭建
- Objective-C - 程式結構
- Objective-C - 基本語法
- Objective-C - 資料型別
- Objective-C - 變數
- Objective-C - 常量
- Objective-C - 運算子
- Objective-C - 迴圈
- Objective-C - 決策語句
- Objective-C - 函式
- Objective-C - 塊 (Blocks)
- Objective-C - 數字
- Objective-C - 陣列
- Objective-C - 指標
- Objective-C - 字串
- Objective-C - 結構體
- Objective-C - 預處理器
- Objective-C - Typedef
- Objective-C - 型別轉換
- Objective-C - 日誌處理
- Objective-C - 錯誤處理
- 命令列引數
- 高階Objective-C
- Objective-C - 類和物件
- Objective-C - 繼承
- Objective-C - 多型
- Objective-C - 資料封裝
- Objective-C - 分類 (Categories)
- Objective-C - 模擬 (Posing)
- Objective-C - 擴充套件 (Extensions)
- Objective-C - 協議 (Protocols)
- Objective-C - 動態繫結
- Objective-C - 複合物件
- Obj-C - Foundation框架
- Objective-C - 快速列舉
- Obj-C - 記憶體管理
- Objective-C有用資源
- Objective-C - 快速指南
- Objective-C -有用資源
- Objective-C - 討論
Objective-C異常處理
Objective-C 透過基礎類 NSException 提供異常處理機制。
異常處理透過以下程式碼塊實現:
@try − 此程式碼塊嘗試執行一組語句。
@catch − 此程式碼塊嘗試捕獲try程式碼塊中的異常。
@finally − 此程式碼塊包含始終執行的一組語句。
#import <Foundation/Foundation.h> int main() { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *array = [[NSMutableArray alloc]init]; @try { NSString *string = [array objectAtIndex:10]; } @catch (NSException *exception) { NSLog(@"%@ ",exception.name); NSLog(@"Reason: %@ ",exception.reason); } @finally { NSLog(@"@@finaly Always Executes"); } [pool drain]; return 0; }
2013-09-29 14:36:05.547 Answers[809:303] NSRangeException 2013-09-29 14:36:05.548 Answers[809:303] Reason: *** -[__NSArrayM objectAtIndex:]: index 10 beyond bounds for empty array 2013-09-29 14:36:05.548 Answers[809:303] @finally Always Executes
在上面的程式中,由於使用了異常處理,程式不會因異常而終止,而是繼續執行後續程式。
objective_c_foundation_framework.htm
廣告