Exceptions Handling and best practices:

When it comes to exception handling there are many opinions people throw out. The couple of below questions come to mind when we talk about exceptions in general.
1. What is exception?
    Exceptions are referred to unexpected or exceptional situational error would occur in the application.

2. When to use exception?
Use exception whenever the code is expected to spit unexpected error.

3. How to use exception?
    A. Should I use Exception object or specific exception
    B. Should I use throw or throw ex
    C. Should I catch the exception every time
   As the name suggest exceptions must be used when there is chance of unexpected error to be occurred:
Firstly, there are two things to keep in mind.
1. Use Try /Catch blocks when you are expecting error to be occurred very rarely, as the  error could be in exception manner; you don’t want to use code preemptive validation to be executed in the normal case.
2. Use code preemptive validation if the exceptions are expected to occur every time when you execute the routine. This way we can avoid having try/catch block to be executed every time.
The thumb rule is to always to use the specific exception and never ever eat the exception unless you have a very good reason. There are cases like you know the errors are not being adversely affected to the application.
Should I use throw ex or throw? It really depends.
The difference between the two is: “throw ex” – this would just assumes that the exception is occurred in the current method where it is being used and doesn’t maintain any of the stack trace information down the other method calls, in case exception occurred, from the current method.
“throw”-It is similar to “re-throw” in the JAVA world, which is unlike the “throw ex” which would keep the stack trace information all the way down to the other method being called, in case of exception, from the current method.

There are three ways to “re-throw” an exception:
catch (Exception ex)
{
 throw;
}
The above preserves the call stack of the original exception.
catch (Exception ex)
{
 throw ex;
}
The above eats the original exception chain and begins a new one.
catch (Exception ex)
{
 throw new MyException (“blah”, ex);
}
The above adds the original exception to the InnerException of a new chain. This can be the best of both worlds, but which one is correct is highly dependent on what you need.
Should I catch the exception every time – From the reading so far it is clear that the exception must be used only in exceptional cases.

4. Using try/catch is a costly operation, is it a myth?
     It is a myth if you just say the above sentence, but there is more to it. The try block would not cost anything; only the catch block would be expensive if it gets executed. The reason is the CLR makes an effort to create exception object and has to maintain the while stack information depending on the situation.
      Imagine if you have a try/catch block in every single routine and would hit the catch block most of the time, this would end of creation of objection so many numbers of times. So it is recommended to avoid try/catch if possible with code preemptive validation as stated in one of the above answers.

5. What are the different types of exception supported in .NET?
     Please refer the below link, as not to reinvent the wheel for the lengthy answer 
      http://blogs.msdn.com/b/brada/archive/2005/03/27/402801.aspx
      http://blogs.msdn.com/b/brada/archive/2005/03/04/385531.aspx

6. How and where to log the exception, database, text file, xml, email?

7. What should be the format of the exception logging?
     I would think the exception must be able to say below things as it should help developers to track down with the issue.
 1. Assembly name, method name, parameter names and its values.
 2. Error message and stack trace if possible.
 3. The Severity of the error to respond.

8. What kind of exception must be shown to the end user?
    Only such exceptions must be shown to the user which makes sense to the user, eliminate exceptions which are target to help the developer. You can as well use the severity based exception model and show the errors based the severity/ threshold value.

9. How exceptions help developers to tackle the issue?

10. Should I write my own exceptions block or use third party one like log4net or EntLib?

11. Can I have multiple catch blocks?

12. What is the importance of Finally block?
“finally” block is used to clean up the code as this block would be executed no matter what, which would guarantee that the resources would be cleaned up.

13. How exceptions works in the chain of method calls?
       When the exception is thrown it passed up the call stack. When the MethodA calls the MethodB, and if MethodB raises the exception, MethodA is given the opportunity to handle the exception. Specifically, the methodA could do one of two things; either it could handle in response to the exception or rethrow so that other method up in the call stack could handle it.

14. What is the relation between the exception and pdb files, how effectively exception must be used in release mode?
In the debug mode, it’s quite handy to trace the errors than in release mode. The pdb files helps in pointing to the exact file and, line number where error has been occurred even if there is no exception handling mechanism in place.

15. Which are the widely used free logging third party libraries?
      There couple of widely used third party libraries.
      1. Log4Net
      2. Enterprise Library
      3. Error Logging Modules and Handlers for ASP.NET
  Exception handling in ASP .NET
 http://support.microsoft.com/kb/306355

16. How to catch unhandled exception
      If the exception is not handled by any of the methods in the application, it would raise  up to the runtime. The event Application.ThreadException is used to handle unhandled exception.

17. How exception works in threading world?

18. Well, well too many question to answer. Let’s get into the business of answering.

19. Can I have multiple catch blocks?
      Yes you can have the multiple catch blocks. The order of the execution depends on the order of the catch block in the code, it is top to bottom.

20. How to catch unhandled exception

Advertisements