Re: Frustration with exceptions
- From: Andrea Desole <news@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 02 Jun 2006 09:53:07 +0200
Leslie Sanford wrote:
Well, I don't suppose this is an object oriented issued perse,
nevertheless...
I've become increasingly frustrated with using exceptions as a means of
handling errors. Let me give you an example. Suppose you have a class
called Sender. It has a method called Send that takes an object
representing a message of some kind. The Sender takes this message and
sends it somewhere. For this example, it doesn't matter where.
Sometimes something can go wrong, an exceptional situation can arise when a Sender attempts to send the message passed to it. It seems appropriate for the Sender to throw an exception in those cases.
right
However, the object passing messages to a Sender is not interested nor is equipped to handle those exceptions. Assuming it has kept its contract by passing a valid message, its responsibility ends after giving it to the Sender.
not really. The fact that the sender can throw an exception means that it's unable to handle a specific situation. So its client has the responsability of
1) handling the situation itself
2) giving the error to someone else, because it is also unable
Now, number 2 is a bit tricky, because not all languages force the client to specify the exceptions it can throw, but I would say the client has in turn to tell its clients what can go wrong
I was thinking that an alternative would be to have the Sender raise an event or call a registered callback when an exception situation arises. That way those who are in the position to handle the exception can register themselves with a Sender and respond appropriately when a Sender encounters an error.
that would imply that your client's clients know that the client is using Sender, exposing implementation details.
Also, you would have no guarantee that an event has a callback. What do you do in that case? Would you define a generic callback for the case the event has no callbacks registered? With exceptions you can have a compiler error (if the language is good enough)
And a couple of minor questions: if more classes register with the sender, what callbacks should be called? All of them? And after the callback has been called, where should the execution resume from? From the point where the error occurred? Should I then check for an error after every function call?
Exceptions do have some advantages :-)
.
- References:
- Frustration with exceptions
- From: Leslie Sanford
- Frustration with exceptions
- Prev by Date: Re: Head First Design Patterns
- Next by Date: Re: Frustration with exceptions
- Previous by thread: Frustration with exceptions
- Next by thread: Re: Frustration with exceptions
- Index(es):
Relevant Pages
|