#import <UIKit/UIKit.h> extern NSString *const EOCErrorDomain; typedef NS_ENUM(NSUInteger,EOCError){ EOCErrorUnknown = -1, EOCErrorGeneralFault = 100, EOCErrorBadInput = 300, }; @interface ViewController : UIViewController @end #import "ViewController.h" /** * 1.ARC在默認狀況下不是「異常安全的」 * 2.OC語言只在極其罕見的狀況下拋出異常,異常拋出以後,無需考慮恢復問題,並且應用程序此時也應該退出。 * 3.在出現「非致命錯誤」時,令方法返回nil/0,或者用NSError以代表其中有錯誤發生。 * 4.錯誤範圍應該定義成NSString的全局常量,而錯誤碼則定義成枚舉爲佳 */ NSString *const EOCErrorDomain = @"EOCErrorDomain"; @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSError *error = nil; // error domain(錯誤範圍,其類型爲字符串) // error code(錯誤碼,其類型爲整數) // error info (用戶信息,其類型爲字典) id object; BOOL ret = [object dosomething:&error]; if (error){ //There was an error } } -(BOOL)dosomething:(NSError**)error { return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end