If you come from the Java background, exception handling in Objective-C seems strange at first. In fact, you usually do not use NSException for your own error handling. Use NSError instead, as you can find it at many other points through the SDK when handling unexpected situations (such as URL operations).
Error handling is performed (approximately) as follows:
Write a method that takes a pointer to an NSError as a parameter ...
- (void)doSomethingThatMayCauseAnError:(NSError*__autoreleasing *)anError { // ... // Failure situation NSDictionary tUserInfo = @{@"myCustomObject":@"customErrorInfo"}; NSError* tError = [[NSError alloc] initWithDomain:@"MyDomain" code:123 userInfo:tUserInfo]; anError = tError; }
The dictionary of user information is the place where you need to put any information with an error.
When you call a method, you check for such an error ...
// ... NSError* tError = nil; [self doSomethingThatMayCauseAnError:&tError]; if (tError) { // Error occurred! NSString* tCustomErrorObject = [tError.userInfo valueForKey:@"myCustomObject"]; // ... }
If you call the SDK method, which can lead to " NSError != nil ", you can add your own information to the userInfo dictionary and pass this error to the caller, as shown above.
source share