Introduction

Exception management is one of the key area for all kinds of application development .You should adopt an appropriate strategy for exception management to build high quality and robust application .It is a very powerful concept and makes the development work very easy if its used efficiently. Inappropriate way of handling exception can degrade the performance. Before dig into this its very important to know what is an exception? The general meaning of "an exception is the breaching of predefined assumption of the application". Remember exception and error are not the same. To explain this let me explain couple of examples.

Example1

Lets say you are try to log data to a file and your application assumes the file is present in the target path , but if is not then exception would be raised .On the other hand if your job is to trace the file and if it is present then log the data in this scenario raising an exception is a bad coding practice. It should be handled through validation code.

Example 2

Lets say in a normal ASP.NET application you are trying to update all necessary fields to the database , and your application assume the database connection is available suppose the connection is not available then raising an exception is a ideal solution. On the other hand while update the mandatory fields in database and few of them having the null values then raising an exception is not necessary it should be handled through validation code.

How to Handle

As a developer you must take the advantage of structured exception handling mechanism through try, catch and finally block .The .NET framework provides a broad hierarchy of exception classes to handle the different types of exception and all these classes derived from Exception class (base class). The developer can extend exception mechanism through inheritance , it helps to implement the custom error handling or provide a proper gateway to handle the complex error handling for the application. Unfortunately, many developers misuse this architecture capability .One very important thing you should keep in mind is how to react when a exception occur at runtime. The good approach to react the exception is

The exception management architecture of a application capable to

  • Detect Exception
  • Perform code clean up
  • Wrap one exception inside another
  • Replace one exception with another
  • Logging and reporting error information
  • Generating events that can be monitored externally to assist system operation

At the beginning of the design you must plan for a consistency and robust exception management architecture and it should be well encapsulated and abstract the details of logging and reporting throughout all architecture layer of your application .Lets discuss some of the best practices of exception

Best Practices

The following list contains tips/suggestions to be consider while handling the exceptions:

For more information

  1. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp

  2. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconbestpracticesforhandlingexceptions.asp