Fail-Fast Pattern/Policy – Fail early with context

How often have you had to fix a run-time error and taken hours or even days to track down the root cause? Using the Fail-Fast policy can all but eliminate such troubles.


The key concept is to fail early, that is as soon as you can programmatically detect bad data exists throw an exception reporting useful contextual information.


e.g.
using System.Reflection;
…..
DataRow drWorkingData = someoneElseClass.FindTheRow(iPrimaryKey);
if (drWorkingData == null)
 throw new ApplicationException(
  “Failed to find data for Primary Key >” + iPrimaryKey + “<“ +
  “[“ + MethodInfo.GetCurrentMethod().DeclaringType.FullName +
  “.” +  MethodInfo.GetCurrentMethod().Name + “()]”);


In this example had we not used a Fail-Fast policy the code would have continued until drWorkingData was referenced, at which point the run-time would automatically throw a null object exception. If you have debugged much code in your life, you will realize that this error could be thrown quite a distance from the original source of the error.


Note the use of reflection to obtain the location of the error. This is my most prevalent cut-and-paste reuse. An obvious improvement would be to encapsulate it in a utility class, and walk a step up the call-stack to report the method’s class and name. That is beyond my knowledge of .Net Reflection.


Also note that instead of using ApplicationException I always use a custom Exception called FailFastException which makes it very easy to trap such errors in a central location and report/ log them in a consistent fashion.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Fail-Fast Pattern/Policy – Fail early with context

  1. Sean's Blog says:
  2. Sean Schade says:

    Excellent point Paul. Exceptions are expensive to throw, but hours or even days of debugging are much more expensive! I have never been a fan of just letting an exception bubble up to the calling code. Usually the context of the exception is so irrelevant at that point that it becomes useless.
    <br>
    <br><a target="_new" href="http://blog.magenic.com/seans/archive/2004/08/21/301.aspx">http://blog.magenic.com/seans/archive/2004/08/21/301.aspx</a&gt;

  3. Jerry Dennany says:

    You mentioned walking up the call stack to get the previous MethodInfo. I demo something like this with Tracing in an article here:
    <br>
    <br><a target="_new" href="http://www.codeproject.com/csharp/customtracelistener.asp">http://www.codeproject.com/csharp/customtracelistener.asp</a&gt;
    <br>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s