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: Tue, 12 Jun 2007 10:05:20 -0400
- Importance: Normal
Hi Nima,
Thank you for your reply. So, exceptions should be reserved for problems
that are serious or fatal such as hardware failures? Which means I need to
decide which of my methods qualify for those cases, if any. But then
questions arise in my mind when I consider that the Cocoa frameworks throw
an exception if I try to access an element of NSArray outside the length of
the array. So, should I implement the same line of thinking whenever my
method's parameters are invalid? Probably not. I think the answer lies by
having a good balance between exceptions and returning nils/zeros from
methods that fail.
I agree with you that exceptions should be kept to a minimum.
Nelson
-----Original Message-----
From: Nima Talebi [mailto:email@hidden]
Sent: Monday, June 11, 2007 8:25 PM
Hi Nelson,
This is a very short and generic answer, and by no means answers you
questions in detail, but from what I understand, exceptions should be used
where an unexpected event could occur, for example... If a user is to save a
document to disk, the control structures should look after the details such
as
1. is the file name valid
2. is the destination path writeable/readeable
...however, say the disk dies while the data is being written... that shold
be handled by an exception.
I'm by no means an expert myself, just sharing a thought. Hope it helps.
Nima
On 6/12/07, Nelson Santos <email@hidden> wrote:
Hi everyone,
Does anyone know (or know of any books or articles) about the proper
way to implement error handling, particularly in Cocoa development?
Development in general is fine too.
I am looking for information about:
1) The proper/standard techniques in developing classes that are
bulletproof.
2) When to properly use logging vs. assertions vs exceptions, or
combinations of the three.
3) At what layer should exceptions be caught.
4) Which error levels should be reported to the user.
5) General strategies and limits of defensive programming.
This came to mind as I was developing my classes. Say, for example,
I have a method that receives an int parameter with an allowed range
from 1 to 1000. How do I make sure I don't mistakingly send it a
value of 5000? Well, I could implement an assert to warn me.
Great! Well, what if I miss something during my development and
testing and an incorrect value gets sent to the method? Then I
should probably implement an IF statement or something to prevent the
method from running. Or maybe raise an exception? Well, that means
catching the exception somewhere to handle it. Should the user be
notified of it? Do I have to do this for every method that I write
that receives parameters? There could be many. Is that efficient?
If so, does every method call now need to be in between try/catch?
These questions and more pop up in my head and I'm not sure how to
answer them. There must be a general recipe that most developers use
when implementing good error handling. Thanks in advance for any help.
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
_______________________________________________
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