When to use exception handling?
When to use exception handling?
- Subject: When to use exception handling?
- From: Helen Cooper <email@hidden>
- Date: Sat, 28 Nov 2009 13:29:14 -0800 (PST)
I'm trying to understand when to use exception handling when developing Cocoa applications. In regard to this, two points stand out to me in Apple's documentation:
"exceptions are resource-intensive in Objective-C"
"Conditions giving rise to exceptions are due to programming errors; you should deal with these errors before you ship a product."
Taken together, I am thinking that:
1. it makes sense to use exception handling to force myself to think about likely exceptional conditions
2. however, final production code should not contain exception handling routines, as I should instead eliminate the potential for exceptions to occur
Just as some further context, here is what I believe I understand from the documentation distinguishing exceptions from error conditions (just to be clear about what I am talking about):
Error objects and exception objects serve different purposes and should not be confused:
- Exceptions (represented by NSException objects) are for programming errors, such as:
- when there is an attempt to access a collection element out of bounds
- when there is an invalid method argument
- when a resource is relied upon but is unavailable (such as an uninitialized global variable for instance)
- when an unimplemented method is called
- out-of-bounds collection access
- attempts to mutate immutable objects
- sending an invalid message
- losing the connection to a server
- In contrast, errors (represented by NSError objects) are for runtime errors, such as:
- when a file cannot be found
- a string in a certain encoding cannot be read
Anyone care to validate my thinking that production code should generally not contain exception handling routines?
_______________________________________________
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