Exception Handling Best Practices


Exceptions plays very important role in any application. You are expected to throw and catch correct exceptions. In case of custom message you can throw custom Exceptions. I am outlining some important points which might help in Exception handling in your application.

  • Fewer Exceptions throws and catches in application makes application runs faster.
  • Nbr of Exceptions thrown and Exceptions thrown per sec in application can be measure by Performance Monitor.

image

  • Use ex as prefix to instance name of Exception. Don’t use e as instance name because it can collide in name with Event Handler instance.
try
{ }
catch (Exception ex)
{ }
  • Use Custom Exception class when you really feel any system exception is not covering your requirement.
  • Custom exception class should be derive from System.Exception or System.ApplicationException. In case of throwing fatal error it is better custom class derived from System.Exception and for non fatal error it is better to derived from System.ApplicationException.
class MyException : Exception
{
   MyException();
   MyException(string message) : base(message) { }
   MyException(string message, Exception innerException) : base(message, innerException) { }
}
  • Don’t throw exceptions if you know you can return values from any method because exceptions are extremely time consuming.
     //Avoid this approach
     int BadWayOfException()
     {
        int i = 0;
        if (i != 4)
            throw new InvalidOperationException("wrong value");
        else
            return i + 3;
     }

     //right way 
     int RightWayOfManagingException()
     {
        int i = 0;
        if (i != 4)
            return 0;
        else
            return i + 3;
     }
  • Don’t catch exceptions inside class libraries unless you rethrow them.
    try
    { }
    catch (Exception ex)
    { 
    throw;
    }
  • For debugging and reporting purposes the last catch block should handle generic Exception object.
    try
    { }
    catch (MyException ex)
    {           
    }
    catch (Exception ex)
    {
    }
  • If you rethrow exception of different type in catch block, ensure that you pass original exception in constructor as inner exception.
    try
    {
    }
    catch (Exception ex)
    {
       //passing inner exception
       throw new InvalidOperationException("my message", ex); 
    }
  • Use XML remarks to document which exceptions a method can throw.
     ///<summary>
     ///<exception cref="InvalidOperationException"></exception>
     ///</summary>
     /// <returns></returns>

 

I hope this document will help you to follow best exception handling practices.


					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: