Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
#DOTNET 33
#.NET 26
#ASP.NET 25
ASP.NET 24
brh 22
#C# 14
.NET 13
WCF 11
c# 9
#MultiThreading 7

Archive · View All
January 2011 10
September 2011 6
May 2011 6
December 2011 5
October 2011 5
June 2011 5
February 2011 3
November 2012 2
August 2012 2
April 2012 2

Exception Handling Best Practices

May 19 2011 1:27AM by Neeraj Kaushik   

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.

  

Tags: #.NET, #DOTNET, DotNet, #C#, brh,


Neeraj Kaushik
54 · 4% · 1132
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Exception Handling Best Practices" rated 5 out of 5 by 1 readers
Exception Handling Best Practices , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]