Professional Error Handling
Professional Error Handling
- Subject: Professional Error Handling
- From: Squ Aire <email@hidden>
- Date: Thu, 22 Oct 2009 23:40:46 +0000
- Importance: Normal
As my code base grows I'm starting to think there something wrong with my error handling code. The following is my current error handling method:
MODEL OBJECTS:
I do methods in my MODEL objects, that could result in an error, like any normal person would, namely to have the famous "NSError **anError" parameter. Two kinds of methods fall under this:
1) The error is a result of an error thrown by the Cocoa methods themselves. This is a simple case and is handled simply by doing "anError = theErrorFromTheCocoaObject" and returning either nil or NO from one's method. I'm confident I'm doing nothing wrong here.
2) The error is originated in my own code. This case I'm unsure of. The way I do it currently is something like this:
NSString *desc = @"Unable to set up image view.";
NSString *info = @"Image does not exist at the specified path.";
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
desc,NSLocalizedDescriptionKey,
info,NSLocalizedFailureReasonErrorKey,nil];
*anError = [NSError errorWithDomain:@"com.mycompany.GreatApp.ErrorDomain" code:43 userInfo:d];
And then of course return NO or nil from the method. Is this how the professionals would do it? The first, and only obvious, problem I can see with this is that I'm putting a magic number 43 in there. I determine this magic number by doing a manual project find for "GreatApp.ErrorDomain" and adding one to the greatest error number that already exists. The second problem is of the error domain string. To me it currently feels like good enough to just have one error domain for the whole app since the app is not huge. The only question is where on earth I would put a constant for that string. I have no idea, since this string can occur in various model objects, so I just manually copy-paste this string every time it's needed.
CONTROLLER OBJECTS:
3) This is where I actually act on the errors that occur when model objects are called. In this case I simply do an [NSApp presentError:anError]; and then occasionally an [NSApp terminate:self]; in cases when the error occurs, for example, while the app is launching. Moreover, I tend to throw in NSLog(@"some description of where and why the error occurred.\n" @"Error: %@\n" @"UserInfo: %@", error, [error userInfo]); Do you think this is unnecessary? My thinking was that maybe it's a good idea since I could ask the user to send me his or her logs in case he or she reported a problem to me one day. I might be wrong though.
So, I believe that 1) and 3) are all-in-all fairly good, except I'm unsure about the NSLogs in 3). I also sense some use for reusable helpers methods I could call in 1) and 3) to avoid repetition. Would you do that? Finally, I sense some serious flaws in 2). I need some advice about it. All other advice, and even descriptions of how pro coders do this kind of stuff, would be very appreciated.
_________________________________________________________________
Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_3:092010_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden