Re: Frustration with exceptions



Responding to Sanford...

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

I think the first issue to resolve is whether the exceptional situation is implicitly or explicitly anticipated in requirements. Exception processing provides a means for handling /unexpected/ situations gracefully (i.e., the software is in a state where correct resolution of requirements can no longer be guaranteed). Exception processing is pretty much the only game in town for dealing with unexpected situations like software errors.

OTOH, if the exceptional situation is anticipated in requirements -- either explicitly or implicitly -- then exception processing is the wrong mechanism. One needs to deal with the situation explicitly in the software solution using the same mechanisms one uses to resolve other requirements. IOW, one writes code to resolve the situation. I would argue that anytime one is sending messages in a distributed environment, there is at least an implied requirement that failures can happen beyond the control of the application in hand and the developer must be prepared for that. (I would further argue that well-formed requirements would be quite specific about what the software should do in those situations.)

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.

How one implements the resolution of such requirements is a much larger issue that I would expect to depend on other things than simply the Sender/Message collaboration. The requirements may describe what situations need to be resolved, what needs to be done to resolve them, and <usually> when they must be resolved. But how that resolution is actually implemented is basically a matter of local design.

This approach isn't novel. In an asynchronous environment, it's a far superior approach than simply throwing an exception and hoping for the best. But I'm thinking that it would be applicable in a synchronous situation as well, i.e. the call to Send blocks until finished.

I agree. [I don't have a choice. B-) As a translationist I express /all/ behavior collaborations using asynchronous communications at the OOA level.]

But suppose the application execution context is not inherently asynchronous. Would it make sense to convert the whole application execution strategy to event-based at the 3GL level just to process one or two special situations? Probably not; a synchronous mechanism like a callback would probably work just fine. This is basically my point above that the specific implementation strategy is probably driven by broader considerations.


*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
Pathfinder is hiring: http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH



.



Relevant Pages

  • RE: ASP.NET Exception Handling
    ... protected void Application_Error(Object sender, EventArgs e) ... throw new Exception; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Object reference error in UserControls Load event
    ... Protected Sub Page_Load(ByVal sender As Object, ... Exception Details: System.NullReferenceException: Object reference not set ... declaring the UserControl in my codebehind rather than my *.aspx file, ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: RichTextBox issue
    ... Private Sub ReadButton_Click(ByVal sender As System.Object, ... Catch ex As Exception ... David McCarter ...
    (microsoft.public.dotnet.languages.vb)
  • Re: IMF Trusted sender
    ... You can absolutley enter a valid external smtp address ... configure exceptions to your connection filter rules. ... button or the exception button, depending on wheter you have are using ... >I have a sender who tries to send email to my network, ...
    (microsoft.public.exchange.admin)
  • Re: "Domain of sender address does not resolve" in mail logs
    ... in my mail logs. ... of sender address XXXXXXXX@xxxxxxxxxx does not resolve ... Domain of sender address XXXXXXXX@xxxxxxxxxxxxxxxxxxxxxxx does not ... I use a Netgear DG834 router which is set to get DNS settings ...
    (Debian-User)