Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 14. Exception Handling > Combining Errors and Exceptions

14.6. Combining Errors and Exceptions

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.

Example 14.10. Mixing Error Handling

@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];
}


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial