<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>DiegumZone. Who Wanna Be An Architect? : Exception Handling</title><link>http://blogs.msdn.com/diegumzone/archive/tags/Exception+Handling/default.aspx</link><description>Tags: Exception Handling</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Speaking on Mission-Critical Apps at SATURN 2009 (SEI's Architecture Conference) – May 7, Pittsburgh, PA</title><link>http://blogs.msdn.com/diegumzone/archive/2009/04/10/speaking-on-mission-critical-apps-at-saturn-2009-sei-s-architecture-conference-may-7-pittsburgh-pa.aspx</link><pubDate>Fri, 10 Apr 2009 22:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9543894</guid><dc:creator>diegumzone</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/diegumzone/comments/9543894.aspx</comments><wfw:commentRss>http://blogs.msdn.com/diegumzone/commentrss.aspx?PostID=9543894</wfw:commentRss><description>&lt;TABLE border=0 cellSpacing=3 cellPadding=0 width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD vAlign=top&gt;&lt;A href="http://www.sei.cmu.edu/architecture/saturn/2009" mce_href="http://www.sei.cmu.edu/architecture/saturn/2009"&gt;&lt;IMG title="SATURN 2009 Speaker" border=0 alt="SATURN 2009 Speaker" src="http://www.sei.cmu.edu/architecture/saturn/2009/images/SATURN_speaker_badge.png" width=120 height=240 mce_src="http://www.sei.cmu.edu/architecture/saturn/2009/images/SATURN_speaker_badge.png"&gt;&lt;/A&gt;&lt;/TD&gt;
&lt;TD vAlign=top&gt;
&lt;P&gt;&lt;FONT color=#000080 size=5&gt;&amp;nbsp;Dear Architect,&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; I'll be delivering a session on &lt;STRONG&gt;"Architecting for Highly Available, Scalable, and Reliable Mission-Critical Applications"&lt;/STRONG&gt; in the &lt;A href="http://www.sei.cmu.edu/architecture/saturn/2009/" target=_blank mce_href="http://www.sei.cmu.edu/architecture/saturn/2009/"&gt;5th edition of SEI Architecture Technology User Network Conference&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; Hope to see you there&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sincerely,&lt;/P&gt;
&lt;P&gt;Diegum&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9543894" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Events/default.aspx">Events</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Exception+Handling/default.aspx">Exception Handling</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Application+Development/default.aspx">Application Development</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Infrastructure/default.aspx">Infrastructure</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Scalability/default.aspx">Scalability</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Parallel+Computing/default.aspx">Parallel Computing</category></item><item><title>Making Room for an Exception</title><link>http://blogs.msdn.com/diegumzone/archive/2008/09/18/making-room-for-an-exception.aspx</link><pubDate>Thu, 18 Sep 2008 07:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8956640</guid><dc:creator>diegumzone</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/diegumzone/comments/8956640.aspx</comments><wfw:commentRss>http://blogs.msdn.com/diegumzone/commentrss.aspx?PostID=8956640</wfw:commentRss><description>&lt;P&gt;&lt;FONT style="BACKGROUND-COLOR: #000080" color=#ffffff size=5 face="Garamond, Times, Serif"&gt;&amp;nbsp;A new friend I got,&amp;nbsp;&lt;/FONT&gt; &lt;A target=_blank href="http://blogs.msdn.com/jmeier" mce_href="http://blogs.msdn.com/jmeier"&gt;&lt;STRONG&gt;J.D. Meier&lt;/STRONG&gt;&lt;/A&gt;, is leading a project intended to provide &lt;A target=_blank href="http://blogs.msdn.com/jmeier/archive/2008/09/02/patterns-practices-app-arch-guide-2-0-project.aspx" mce_href="http://blogs.msdn.com/jmeier/archive/2008/09/02/patterns-practices-app-arch-guide-2-0-project.aspx"&gt;renewed Architecture Guidance for the Microsoft platform, and specifically for .NET applications&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A target=_blank href="http://blogs.msdn.com/jmeier" mce_href="http://blogs.msdn.com/jmeier"&gt;&lt;STRONG&gt;J.D.&lt;/STRONG&gt;&lt;/A&gt; was compiling input from different sources and &lt;A target=_blank href="http://www.codeplex.com/AppArch" mce_href="http://www.codeplex.com/AppArch"&gt;launched a web site for his project in CodePlex&lt;/A&gt; (a Microsoft portal for community projects, most of them available with code, samples, screencasts and so on)&lt;/P&gt;
&lt;P&gt;I had the privilege of being enlisted in &lt;A target=_blank href="http://blogs.msdn.com/jmeier" mce_href="http://blogs.msdn.com/jmeier"&gt;&lt;STRONG&gt;J.D.&lt;/STRONG&gt;&lt;/A&gt;' list an one of his inquiries was about my beliefs on &lt;STRONG&gt;exception handling&lt;/STRONG&gt;. &lt;A target=_blank href="http://diegumzone.spaces.live.com/cns!1pHxrrKG6RzuZjEIZgyJyg0A!150.entry" mce_href="http://diegumzone.spaces.live.com/cns!1pHxrrKG6RzuZjEIZgyJyg0A!150.entry"&gt;I had written a blog post three years ago about that&lt;/A&gt; although, ugh!, I did it in Spanish as I was living in South America at that time&lt;/P&gt;
&lt;P&gt;I was about translating my blog post for &lt;A target=_blank href="http://blogs.msdn.com/jmeier" mce_href="http://blogs.msdn.com/jmeier"&gt;&lt;STRONG&gt;J.D.&lt;/STRONG&gt;&lt;/A&gt; but better than emailing my outcome, I considered worth making my ideas of public domain, so here we go:&lt;/P&gt;
&lt;P&gt;There are four common antipatterns (those are, bad practices, bad habits) when dealing with exceptions in today's platforms like .NET and Java. Those are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Exaggerated "exceptionalization"&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Unnecessary intervention&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Negligent inaction&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Inopportune handling&lt;/STRONG&gt; &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Let's review each.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Exaggerated "exceptionalization". &lt;/STRONG&gt;I heard about several software projects where their management thought that exceptions were made to address any abnormal situation. All abnormal situations. That way, data entry modules validated received input and before the first mandatory field left blank, the first entered date field violating the "MM/DD/YYYY" mask, etc, an exception was thrown. Just because the policy was throwing an exception for each abnormal situation &lt;BR&gt;That got the programming cost more expensive as all that plumbing of exception throwing &amp;amp; catching was developer responsibility. But that was just to start: the code became illegible as the treatment of these exceptions was several line of codes further. Occasionally, several entries in the callstack further &lt;BR&gt;So we can say they got two undesired consequences: too much code and illegibility. A better approach in cases like these should have handled validations in situ, giving control back to the user when some field was entered wrongly, in a simple and direct manner (that is, without changing an error for another one) &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Unnecessary intervention.&lt;/STRONG&gt; It's normal having code blocks that call modules that eventually throw exceptions. What I find anti natural -accepting that not everybody thinks as I do- is having to be prepared to react before any possible exception. I remember from my participation in a project at a multinational bank, that enclosing between a &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;try - catch&lt;/STRONG&gt;&lt;/FONT&gt; block every method call that could throw exception was imperative. And it was forbidden doing nothing inside the &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;catch&lt;/STRONG&gt;&lt;/FONT&gt; brackets. To prevent this, the project management staff asked certain development team members to build a robot that reviewed the project source code, hunting for empty &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;catch's&lt;/STRONG&gt;&lt;/FONT&gt;. After some while, the managers got finally convinced about the fact that not all the time there's something to do before an exception, but they just made their position a bit more flexible (and consequently, the robot), by allowing in such cases the chance of documenting inside the &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;catch&lt;/STRONG&gt;&lt;/FONT&gt; an explanation about &lt;EM&gt;why&lt;/EM&gt; not to intervene. Since then, developers in a hurry to finish their part started entering explanations like &lt;FONT face="Courier New"&gt;/* dkjfhashakjbadshkjdkfjdhkj */&lt;/FONT&gt; in empty &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;catch&lt;/STRONG&gt;&lt;/FONT&gt;'s, and the robot has never found a violation of the rule again &lt;BR&gt;The real thing behind this story is that, in any code segment, there will be exceptions thrown by module invocations which sometimes we could handle, so we could act in consequence, sometimes our involvement will be simply senseless. An example of this? The Data Access Layer (DAL) returns &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;System.Data.SqlClient.SqlException&lt;/STRONG&gt;&lt;/FONT&gt; because the connection string was sent with an expired password. The exception is caught in the Business Layer, the one that called the DAL. Does it make sense catching the exception in that place? Maybe to make the answer more obvious, the question could have been "can the Business Layer create a new password for the DAL?" &lt;BR&gt;The right thing in that case is masking, before leaving the DAL, the low-level exception (coupled to ADO.NET in this case) as a high level one without losing the original exception as it's the one that holds the stack-trace). The original exception is usually known as &lt;EM&gt;root cause&lt;/EM&gt; and logged, while the high-level exception is shown to the user. For more info, let's check next &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Negligent inaction.&lt;/STRONG&gt; This is the opposite scenario regarding the previous case, also negative. It happens each time we leave go any occurred exception without taking over not even to mask certain error messages in order to avoid slapping users faces with low-level explanations like the expired password when trying to connect to the database &lt;BR&gt;Eluding any intervention, as a policy, leads to code with unpredictable behavior. That erodes its reliability from a development perspective, and its usability, from the users perspective &lt;BR&gt;However, how to set the correct degree of involvement without falling in the previous anti-pattern? As told before, masking the low-level exception into a higher one before leaving the layer or module where that exception is produced. Special care here, as I previously said, of original exception nesting as the new exception will be useful for presentation purposes (thinking in the user), while the original one will allow to understand what provoked it (when IT responsible personnel takes over) and the first measure in order to prevent these situations in the future, mostly if the cause is a software bug (the example of the expired database password does not belong to that category). Don't you remember having seen a web site where, after clicking in some submit button, you got a beautiful message in red characters, like &lt;FONT color=#ff0000 face="Courier New"&gt;&lt;STRONG&gt;[ODBC Exception, SQL code = ...]&lt;/STRONG&gt;&lt;/FONT&gt;? Wouldn't have been better logging that error while communicating to the user something like "&lt;STRONG&gt;We are experiencing some difficulties. Please try again later or call to our operators at (XXX)XXX-XXXX&lt;/STRONG&gt;" &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Inopportune handling.&lt;/STRONG&gt; A customer of mine reclaimed about the low reliability of MS .NET regarding exception handling. He told me that they, before certain range of exceptions, had a handler invoked in the &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;catch&lt;/STRONG&gt;&lt;/FONT&gt; section. The handler generated a record in a database with the exception, with the end of taking monthly statistics on their software failures. Concretely, his recrimination towards .NET was that, while was generating the record in the database (low-level speaking, acting in a transaction-oriented manner with locking mechanisms, etc) the web user stayed waiting as a silly for an answer that, worst of worst, was abnormal &lt;BR&gt;When checking deeper, we found that the &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;catch&lt;/STRONG&gt;&lt;/FONT&gt; logic was executed by design in the same thread where the exception had been occurred. Therefore, if we are urged to let the user know that we couldn't, but we also want to generate a record for stats that are reviewed in a monthly basis, the advice here is perform simple logging (a plain text file, not even XML), loading the database in offline using, for instance, ETL mechanisms &lt;BR&gt;Even, if stats may be checked anytime and we want to have information updated, let's say, 10 minutes ago or even less, we can forget ETL by applying &lt;EM&gt;fire-and-forget&lt;/EM&gt; mechanisms in the exception handler. For instance, the exception handler can post a message to a queue whose consumer -necessarily another thread or process- will complete the action by registering it into a database. Thus, the handler just post to the queue and returns the control to its invoking &lt;FONT face="Courier New"&gt;&lt;STRONG&gt;catch&lt;/STRONG&gt;&lt;/FONT&gt;, which in turns will finish and return error and control to the user. The persistence logic stays, that way, decoupled and will be executed asynchronously &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I want to invite anyone to share experiences to &lt;A target=_blank href="http://blogs.msdn.com/jmeier" mce_href="http://blogs.msdn.com/jmeier"&gt;&lt;STRONG&gt;J.D.&lt;/STRONG&gt;&lt;/A&gt;, anti-patterns and their respective best practices in exception handling&lt;/P&gt;
&lt;P&gt;PS. An interesting study on Exception Handling is found in &lt;A target=_blank href="http://www.amazon.com/Expert-One-Design-Development-Programmer/dp/0764543857/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1221714012&amp;amp;sr=8-1" mce_href="http://www.amazon.com/Expert-One-Design-Development-Programmer/dp/0764543857/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1221714012&amp;amp;sr=8-1"&gt;Rod Johnson's best seller "Expert one-on-one: J2EE Design and Development" (Wrox, 2003, pages 125-132)&lt;/A&gt;. I strongly recommend&amp;nbsp;its reading&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8956640" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://blogs.msdn.com/diegumzone/archive/tags/Exception+Handling/default.aspx">Exception Handling</category></item></channel></rss>