Click here to Skip to main content
15,867,851 members
Articles / Programming Languages / C#
Article

Best Practices of Exception Management

Rate me:
Please Sign up or sign in to vote.
2.02/5 (28 votes)
8 Mar 20055 min read 146.4K   56   6
Exception management is one of the key areas for all kinds of application development . You should adopt an appropriate strategy for exception management to build high quality and robust applications. It is a very powerful concept and makes the development work very easy if it's used efficiently.

Overview

Exception management is one of the key areas for all kinds of application development. You should adopt an appropriate strategy for exception management to build high quality and robust applications. It is a very powerful concept and makes the development work very easy if it's used efficiently. Inappropriate way of handling exceptions can degrade the performance of your applications.

Introduction

Before dig into the details of exception management, it's very important to know what is an exception? The general meaning of "an exception is the breaching of a predefined assumption of the application". Remember, exception and error are not the same. To explain this, let me explain a couple of examples.

Example1

Let's say you are trying to log data to a file and your application assumes the file is present in the target path, but if it is not, then an 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

Let's say in a normal ASP.NET application, you are trying to update all necessary fields to the database, and your application assumes the database connection is available; suppose the connection is not available, then raising an exception is an ideal solution. On the other hand, while updating the mandatory fields in database and if a few of them are having 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 advantage of the structured exception handling mechanism through try, catch and finally blocks. The .NET framework provides a broad hierarchy of exception classes to handle the different types of exceptions and all these classes are derived from Exception class (base class). The developer can extend the exception mechanism through inheritance, it helps to implement custom error handling or provides a proper gateway for 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 an exception occurs at runtime. The good approach to react to an exception is:

  1. Just ignore the exception or implement different possible catch blocks to catch the exception.
  2. Catch the exception and perform the required action for your application, and if you can not recover from the exception, rethrow the exception.
  3. Catch the exception and wrap with another exception which is more relevant for your application. Exception wrapping is used to avoid breaking the layer abstraction due to exception. For preserving the original exception, you can use the InnerException property of the Exception class. This allows the original exception to be wrapped inside a new exception (which is more relevant for your application). To understand the wrapping of exceptions, let's look at this example inside a method of your application which caught a lower level exception called IOException. You can wrap this original exception with an application level exception called LoadingException or FailtoLoadInfo or something else which is more relevant for your application, rather then alerting the lower level exception to the user.

The exception management architecture of an application should have the capability to:

  • Detect exception
  • Perform code clean up
  • Wrap one exception inside another
  • Replace one exception with another
  • Log and report error information
  • Generate events that can be monitored externally to assist system operation

At the beginning of the design, you must plan for a consistent and robust exception management architecture and it should be well encapsulated and abstract the details of logging and reporting throughout all architecture layers of your application. Let's discuss some of the best practices of exception management.

Best Practices

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

  • Exception is an expensive process, for this reason, you should use exceptions only in exceptional situations and not to control regular logic flow. For example:
    C#
    void EmpExits ( string EmpId)
    {
        //... search for employee
        if ( dr.Read(EmpId) ==0 ) // no record found, ask to create
        {
            throw( new Exception("Emp Not found"));
        }
    }
    

    The best practice is:

    C#
    bool EmpExits ( string EmpId)
    {
        //... search for Product
        if ( dr.Read(EmpId) ==0 ) // no record found, ask to create
        {
            return false}
        }
    }
  • Avoid exception handling inside loops. If it's really necessary, implement try/catch block surrounding the loop.
  • Adopt the standard way of handling exceptions, through try, catch and finally blocks. This is the recommended approach to handle exceptional error conditions in managed code, finally blocks ensure that resources are closed even in the event of exceptions. For example:
    C#
    SqlConnection conn = new SqlConnection("...");
    try
    {
        conn.Open();
        //.some operation
        // ... some additional operations
    }
    catch(…)
    {
        // handle the exception
    }
    finally
    {
        if (conn.State==ConnectionState.Open)
            conn.Close(); // closing the connection
    }
  • Wherever possible, use validation code to avoid unnecessary exceptions. If you know that a specific avoidable condition can happen, precisely write code to avoid it. For example, checking for null before performing any operations on an object/variable can significantly increase performance by avoiding exceptions. For example:
    C#
    double result  = 0;
    try{
        result = firstVal/secondVal;
    }
    catch( System.Exception e){
        //handling the zero divided exception 
    }

    This is better then the above code:

    C#
    double result = 0;
    if(secondVal >0)
        result = firstVal/secondVal;
    else
    result = System.Double.NaN;
  • Do not rethrow exceptions for unnecessary reasons because the cost of using throw to rethrow an existing exception is approximately the same as creating a new exception, and rethrowing an exception also makes it very difficult to debug the code. For example:
    C#
    try {
        // Perform some operations ,in case of  throw an exception…
    } catch (Exception e) {
        // Try to handle the exception with e
        throw;
    }
  • The recommended way to handle different errors in different ways is by implement a series of catch statements. This is nothing but ordering your exception from more specific to more generic. For example, to handle file related exceptions, it's better to catch FileNotFoundException, DirectoryNotFoundException, SecurityException, IOException, UnauthorizedAccessException, and at last Exception.
  • ADO.NET errors should be captured through SqlException or OleDbException.
    • Use the ConnectionState property for checking the connection availability instead of implementing an exception.
    • Use try/finally more often. finally provides option to close the connection or the using statement provides the same functionality.
    • Use the specific handler to capture specific exceptions. In a few scenarios, if you know that there is possibility for a specific error like database related error, it can be caught through SqlException or OleDbException, as below:
      C#
      try
      { ...
      }
      catch (SqlException sqlexp) // specific exception handler
      { ...
      }
      catch (Exception ex) // Generic exception handler
      { ...
      }
  • Your exception management system should be capable to detect an exception, wrap one exception inside another, replace one exception with another, and log and report the exception information for monitoring the application.
  • It's recommended to use “Exception Management Application Block” provided by Microsoft. It is a simple and extensible framework for logging exception information to the event log, or you can customize it to write the exception information to other data sources without affecting your application code and implement all best practices tested in the Microsoft Lab.

For more information:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am Anand Kumar from India my core expertise is developing robust service components using ATL COM ,Remoting and Web Service. I am passionate about performance optimization and best practices of .NET ,Microsoft Patterns and Practices & VSTS . I believe knowledge sharing is the greatest joy ,the philosophy behind knowledge sharing is gain more by sharing more , apart from my regular office work I do involve in various Microsoft User Group forums such as UG-Hyderabad ,Bangalore, Chennai etc. I am one of the core members of Microsoft User Group Hyderabad (MUGH ) and a volunteer member of INETA.


Comments and Discussions

 
QuestionHelp! I've not been able to handle this error! Pin
mnongkhlaw26-Jun-08 3:15
mnongkhlaw26-Jun-08 3:15 
QuestionRegarging try-catch-finally blocks Pin
saswata.purkayastha@gmail.com20-Nov-06 6:44
saswata.purkayastha@gmail.com20-Nov-06 6:44 
AnswerRe: Regarging try-catch-finally blocks Pin
HandyGuy21-Nov-06 22:19
HandyGuy21-Nov-06 22:19 
GeneralRe: Regarging try-catch-finally blocks Pin
saswata.purkayastha@gmail.com25-Nov-06 9:53
saswata.purkayastha@gmail.com25-Nov-06 9:53 
GeneralRe: Regarging try-catch-finally blocks Pin
HandyGuy25-Nov-06 21:43
HandyGuy25-Nov-06 21:43 
The main difference between catch and finally is catch will execute if the application encounter an exception but finally will execute irrespective of exception raised or not , in my opinion finally is the best place to close/dispose your unmanaged objects .


Cheers
Anand
GeneralRe: Regarging try-catch-finally blocks Pin
Bhavik Thakkar,Smiley IT Solution22-Aug-12 0:48
professionalBhavik Thakkar,Smiley IT Solution22-Aug-12 0:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.