Atlanta ASP.Net MVC Developer/ Architect
Fail-Fast Pattern/Policy
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.
| Print article | This entry was posted by Paul Lockwood on July 25, 2004 at 8:57 am, and is filed under Development (General). Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 6 years ago
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.
about 1 year ago
Yup throwing exceptions is expensive, I found that out the hard way. When first writing .Net code I used Exceptions to indicate ‘expected errors’ – i.e. broken business rules, validation errors etc which happen all the time. Not a wise idea!
As I understand it the main point with Fail-Fast is to throw an exception when the code would fail anyway. In almost every case I use the technique it is in places that an error SHOULD never happen, when some inevitably do using fail-fast prepares the ground for spotting the root cause.
Allowing Exceptions to bubble up the code [wry smile] That’s always a good one to get developers arguing especially Java people who generally miss their checked exceptions.
about 1 year ago
You mentioned walking up the call stack to get the previous MethodInfo. I demo something like this with Tracing in an article here:
http://www.codeproject.com/csharp/customtracelistener.asp