Re: Frustration with exceptions



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 :-)
.



Relevant Pages

  • Re: RBL filtering configuration
    ... Connection Filtering> Exceptions and hit Help it is stated that this ... Exchange Server checks the Global ... > then returns the following error message to the sender: ...
    (microsoft.public.exchange2000.admin)
  • RE: Application.ThreadException not working
    ... been caused by exceptions in threads, most likely due to trying to ... void CurrentDomain_UnhandledException(object sender, ... private void exceptionButton_Click ... Which is just a windows form that has 2 buttons, crossThreadButton & ...
    (microsoft.public.dotnet.framework)
  • Application.ThreadException not working
    ... been caused by exceptions in threads, most likely due to trying to ... void CurrentDomain_UnhandledException(object sender, ... private void exceptionButton_Click ... Which is just a windows form that has 2 buttons, crossThreadButton & ...
    (microsoft.public.dotnet.framework)
  • Re: Object reference not set to an instance of an object.?? Please Help
    ... I hate to be picky, but have you actually stepped through your code in an ... IDE and evaluated the variables being used? ... Unlike some other exceptions, ... > private void Page_Load(object sender, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Where shoul I throw RuntimeException
    ... country is ruled by a committee, be in that committee" being an expat from a ... idea to let the client of this code deal with inners of the persistence API, ... In EJB 3.0 the choice of checked vs. unchecked exceptions has strong ...
    (comp.lang.java.programmer)