Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Exception Handling in Enterprise Applications  »  Suggestions 1-4
 »  Home  »  .NET Newbie  »  Exception Handling in Enterprise Applications  »  Suggestions 1-4
Exception Handling in Enterprise Applications
by Scott Rutherford | Published  02/28/2005 | .NET Framework .NET Newbie | Rating:
Suggestions 1-4

1. When throwing exceptions, always include the source exception if one exists, by setting the InnerException property.

You never know how an exception is going to be dealt with at higher levels, so it is best to provide as much information as possible regarding the state and cause of the exception.

[Visual Basic]
        Try
        Catch e As Exception
            Throw New ApplicationException("Uh oh...", e)
        End Try 

Practice 5, below, shows you how to access and use this InnerException at higher levels

2. Uniquely identify error locations in code, and group into types.

This will allow a link from the ultimate destination of the error message (e.g. the Event Log) back to source code. The simplest way to do this is to share an enum with all source code that provides a code lookup, which I call EventID:

[Visual Basic]
    Public Enum EventID As Integer
        DATA_ERROR = 1
        FILE_ACCESS_ERROR = 2
        REGISTRY_ACCESS_ERROR = 3
        FTP_AUTHENTINCATION_FAILURE = 4
        FTP_UNKNOWN = 5
        LOG_ERROR_UNKNOWN = 6
        SYSTEM_CONFIG_MISSING_ARGS = 7
        SYSTEM_INFORMATION = 8
        ZIP_OPERATION_FAILURE = 9
    End Enum 

3. Identify the importance of the error

Basically, you need to decide if the error encountered is going to stop the program from doing what it is expected to do. There are three possible values that indicate error importance: critical, warning, and information. Again, the simplest way to identify this is to share an enum, which I call EventImportance:

[Visual Basic]   
 Public Enum EventImportance As Short
        Critical = 1
        Information = 2
        Warning = 3
    End Enum
Here is an example of the distinction:
A network error is encountered because a required path cannot be found. This could mean that the task will never be completed because the application does not have access to read the directory, in which case the error is critical. It could also mean that the operation timed out because of heavy traffic but would presumably succeed on a subsequent pass, in which case the error is a warning (perhaps something could be done administratively to resolve the issue, perhaps not). However, the path may be unavailable simply because it has been removed and is no longer required, in which case the error is purely informational.

Note: you could use the .NET system enumeration System.Diagnostics.EventLogEntryType here, although it might be more flexible to support your own custom enumeration.

4. Act according to the urgency of the error

Urgency is whether or not the program can continue to function in light of the error encountered. This is generally independent of the importance, and it is this issue that will dictate how you handle the error.

  • If an error is urgent, it needs to be raised so that the current process stops, or at least handles it at a higher level.
  • If not, it can be logged (according to its importance) and processing can continue.

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