Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Academic arguments about the superiority of exceptions and traditional error handling will continue to rage on. In the meantime, I'm a big fan of combining the two. NSError objects have clear advantages in complex applications that must be localized. Java-style exception handling has clear advantages for simplifying code and execution flow. Listing 14-10 shows one way in which traditional C error handling, NSError objects, and exceptions can be combined.
@try {
const char *path = ...
int fd = open(path,O_RDONLY);
if (fd<0) {
NSString *errorPath = [NSString stringWithCString:path];
NSDictionary *info = [NSDictionary dictionaryWithObject:errorPath
forKey:@"Path"];
NSError *error = [NSError errorWithDomain:NSPOSIXErrorDomain
code:errno
userInfo:info];
@throw error;
}
...
close(fd);
} @catch (NSError *error) {
[self presentError:error];
} |