Re: Error and Exception Handling: When to use
Re: Error and Exception Handling: When to use
- Subject: Re: Error and Exception Handling: When to use
- From: Nelson Santos <email@hidden>
- Date: Thu, 14 Jun 2007 22:25:22 -0400
Hi all,
Me again. Once again thanks to everyone's input on this subject. I
finally had some time to play around with exceptions and error
handling, but I still have a question. I'll use a quick example to
illustrate:
// Car.h:
#import <Cocoa/Cocoa.h>
typedef enum
{
DOORS_TWO = 2,
DOORS_FOUR = 4
} CarNumDoors;
@interface Car : NSObject
{
CarNumDoors _numDoors;
}
- (void)setNumDoors:(CarNumDoors)numDoors;
@end
// Car.m:
#import "Car.h"
@implementation Car
- (void)setNumDoors:(CarNumDoors)numDoors
{
NSAssert(numDoors >= DOORS_TWO && numDoors <= DOORS_FOUR, @"Invalid
number of car doors.");
_numDoors = numDoors;
}
@end
Calling code is this:
Car *newCar = [[Car alloc] init];
[newCar setNumDoors:8]; // Oh oh! Someone didn't call the method
properly!
When running this program with the debugger active, it's beautiful.
The debugger halts when the Assertion is raised. Excellent. Now,
let's say this code slipped through the testing phase of the project
and now sits in a release version being distributed. I have
confirmed that the assertion is still triggered in the release
version (because the instance variable is not changed) and it appears
that the current event loop is cancelled and the program carries on,
as Mr. Uli Kusterer has said. It also appears that NSApplication
catches the exception thrown by NSAssert at the top level, since the
application doesn't close (as Ryan Homer has said.)
That all cool but it doesn't help me, the developer, know that there
is still a problem in the code. I would think the best approach
would be to inform the user that an exception was raised and maybe
click on a button to report the bug to my web site, and then continue
running. How would I go about doing that? Is there a single logical
place I should implement that? I don't want to rely on my testing
abilities to make sure that each and every method works the way they
were designed. After all, I may not be the only one using my code
and I'm sure the next guy would appreciate knowing that there aren't
little gaps in the code that can take forever to figure out.
Does anyone else write their programs with these things in mind or am
I thinking too deeply? Does anyone else have a different idea?
Thanks.
Nelson
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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