#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