Re: Looking for Tips/Writeup on overall approach to Exception Processing
- From: "Tim Rowe" <news@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 9 Jun 2007 15:44:44 +0100
"Rex" <RexForum4453@xxxxxxxxxxxxxxxxxx> wrote in message
news:2pug63dvpatk2je52gk6l6nnbbjhg6gm1j@xxxxxxxxxx
Re: Looking for Tips/Writeup on overall approach to Exception
Processing
I think it would be useful for you to have a look at "Design By Contract".
Although I have reservations about it as a design approach, the concept of
contracts on which it is built is a good way of thinking about how to use
exceptions.
So here goes: Let's say most or all of my Application's methods
return a bool to indicate whether the routine successfully completed
or not. The bool will be equal to "false" ONLY if there is a logic
error or system error - never due to some invalid input from the User
and never, for example, because a User file is not found. Does it make
sense to do it this way?
Probably not. Think about what the method should do, and what conditions
have to apply for it to succeed in doing that. Document those conditions
(the "preconditions")and require the client to make sure they apply, at
least insofar as the client can. For debugging check that the client /has/
met the preconditions and throw an exception if they haven't. If it's an
exposed interface (eg, a library interface) consider leaving the check in
the the released version. Debugging or not, if the preconditions are met and
the method fails to do what it promises, raise an exception.
That's not the be-all and end-all of exception handling, but it's a good
start.
And if so, should the calling routine always
check for this "false" value on all called methods??? (which could be
alot of code!)
Which is one big advantage of exceptions.
And should the error message that the User sees always
come from the called routine?
Another big advantage of exceptions is that they can be handled at an
appropriate level. A low level method might have no idea about what it's
being used for, and so has no change of producing a message that's
meaningful to the user in the context of what they were trying to do. Let
the exception propogate up to a higher level, and the application can
account for what it was trying to achieve with the lower level call, and
what it means to the user. Then you can give an error message in the
/user's/ terms, not in the /program's/ terms (if the error looks like being
a logic error, though, consider making the exception trace available to the
user in a form that they can easily report back to you, such as an error log
file that they can email to you).
Any error message that a low-level method can generate is almost certainly
useless to a user, and will probably only serve to annoy them.
And does this mean I would implement a
Try block in every called method??? I would very much appreciate
anyone who can help me sort all of this out... which brings me to...
No. You only catch an exception if you are in a position to do something
useful with it.
3. Does anyone know of any document on the Web with some tips and/or
suggested overall approaches to Exception Processing?
This thread? ;-)
.
- Follow-Ups:
- Re: Looking for Tips/Writeup on overall approach to Exception Processing
- From: Michael A. Covington
- Re: Looking for Tips/Writeup on overall approach to Exception Processing
- References:
- Prev by Date: Re: server scenario - variables in the right spot?
- Next by Date: Re: Looking for Tips/Writeup on overall approach to Exception Processing
- Previous by thread: Re: Looking for Tips/Writeup on overall approach to Exception Processing
- Next by thread: Re: Looking for Tips/Writeup on overall approach to Exception Processing
- Index(es):
Relevant Pages
|