Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Exception Handling in Enterprise Applications
 »  Home  »  .NET Newbie  »  Exception Handling in Enterprise Applications
Exception Handling in Enterprise Applications
by Scott Rutherford | Published  02/28/2005 | .NET Framework .NET Newbie | Rating:
Scott Rutherford
Scott Rutherford is a consultant and .NET developer who has developed Enterprise business applications in many fields including Restaurant, Market Research, Automotive, and Analytical Chemistry. He is an Microsoft Certified Professional with the .NET Platform. 

View all articles by Scott Rutherford...
Introduction
This article assumes a general understanding of Structured Exception Handling in .NET (VB or C#).
This article is supplemental to the MSDN article: Best Practices for Handling Exceptions (read this!!!).

Exception handling is often understood to mean "exception stopping". That is, make my program continue to run without stopping and without requiring human interaction. However, exceptions can be lifelines that help you maintain the health of a dynamic Enterprise application. Often you will want to track and respond to an application that is not self-contained in a single process, or on a single machine.

In this article, I suggest eight "Best Practices" to implement in Enterprise Applications, with code examples to show how it's done. If you consider the suggestions below early in the development process, and follow these practices in all applications, you will reap the benefits on the Enterprise scale.

  1. Always include the Source Exception, using InnerException
  2. Uniquely Identify error locations in your code
  3. Identify the Importance of the error
  4. Act on the Urgency of the error
  5. Handle errors Centrally, but use available Context
  6. Report errors to Windows Event Logs
  7. Ensure all apps can Write to Windows Event Logs
  8. Use Enterprise Tools to identify program errors

The basic rule is to ensure your exceptions are raised with codes that are programmatically, or at least systematically, readable. And always use the Windows Event Logs to report errors.  This will allow you to use Enterprise tools to monitor and respond to logged errors. The main benefit of following these practices is that all program errors are centrally reported and uniquely identified. This gives Enterprise administrators the power to identify and respond to all program errors.

    Here are some examples of real life applications in which I implement these rules:
  • An ASP.NET application that opens a window to the Enterprise for outside clients
  • Various desktop apps
  • Windows Service that picks up data files from legacy apps at remote sites
  • Data broker component that provides database access in various ASP.NET applications
  • Windows Service that monitors processes and log files running on remote machines

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 03/08/2005)
Rating
I have yet to put some time in practicing this and the content looks good for producing custom errors. I would like to see your ideas on how to deal with SQL/ODBC type errors in addition to these.
 
Comment #2  (Posted by (The Author) on 03/22/2005)
Rating
For ODBC errors, I catch these in the calling application and report as any other error described in the article. For SQL exceptions, I catch them inside the Stored Proc, and report a meaningful error back to the calling app.
 
Comment #3  (Posted by an unknown user on 08/24/2005)
Rating
Your article is basically focused on logging the event. You should take a look at log4net http://logging.apache.org/log4net/ as a tool it provides both logging level and application code context. It has a multitue of logging connectors (including Event Log.) that can be used simultaneously to log messages within your applications.
 
Comment #4  (Posted by Pawel Pabich on 08/25/2005)
Rating
Hajo,
I do not aggre that usage of enums is good practice.
You should rather create separate class for every type of error.

Pawel
 
Comment #5  (Posted by Piers Lawson on 08/25/2005)
Rating
As mentioned by another contributor, there are frameworks to help with this, such as log4net. An alternative that provides logging and exception handling is the Enterprise Library (EL) available for free from Microsoft. See msdn.microsoft.com/library/en-us/dnpag2/html/entlib.asp

Both these frameworks allow centralised logging to be configured both in terms of destination and levels. The EL also provides other well integrated features such as exception handling, data access, caching, configuration file handling.

I wrote an article introducing the Enterprise Library's logging features here: http://www.codeproject.com/dotnet/GetLoggingWithEntLib.asp
 
Comment #6  (Posted by Jparlato on 08/25/2005)
Rating
Thanks for putting this together. We can obviously choose variations for logging and other aspects, but this was useful - as is.

 
Comment #7  (Posted by Oli on 12/12/2005)
Rating
Great. Thanks....
 
Comment #8  (Posted by an unknown user on 08/09/2006)
Rating
Skimmed over the very basics without going into any detail. Good for juniors i suppose.
 
Sponsored Links