latest Post

Coding Standards and Guidelines: Error Handling in C#

1. Error handler should be present whenever you anticipate possibility of error.
2. Do not use Try-catch for flow- control.
3. Never declare an empty catch block.
4. Error Message should be user friendly, simple and understandable.
5. Errors should be raised in the routines present in the components and captured in the application’s GUI.
6. Use Try-Catch statements in each and every function you write. Adhere to it strictly with out fail.
7. Catch only exceptions for which you have explicit handling.
8. In a catch statement that throws an exception, always the original exception(or another exception constructed from original exception) to maintain the stack location of the original error:
catch (Exception exception)
{
   
Messagebox.Show (exception.Message);
   
throw;     //same as throw exception;
}
9. Avoid error code as method return values.
10. Avoid defining custom exception classes.
11. When defining custom exceptions:
  • Derive the custom exception from Exception.
  • Provide custom serialization.
12. Use dispose on types holding scarce resources, i.e., files, database connections.

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment