Tuesday, July 17, 2007 3:24 PM
kolbis
Do you know how to handle exceptions?
Exception handling is not an exact science, however there are some guidelines that one should follow. I have constructed a list of rules that in my opinion is common to all applications in terms of exception handling.
- As a rule, all exceptions in the application must be handled at the business layer level.
- Custom exceptions will be created to handle any application exceptions.
- Exceptions that may require special handling, a handler should be created.
- When Exception is handled an ID should be generated for that exception.
- Exceptions that may surface to the presentation layer must be user friendly and should not contain information about the inner exception; however the exception ID should be displayed to the user so that the support team can locate the specific exception and review it faster and easier.
- Never do a 'catch exception and do nothing'. If you hide an exception, you will never know if the exception happened or not. Lot of developers uses this handy method to ignore non significant errors.
- You should always try to avoid exceptions by checking all the error conditions programmatically. In any case, catching an exception and doing nothing is not allowed. In the worst case, you should log the exception and proceed.
- In case of exceptions, give a friendly message to the user, but log the actual error with all possible details about the error, including the time it occurred, method and class name etc.
- Always catch only the specific exception, not generic exception.
So, based on those rules, we can expand and think about other related exception handling.
תגים:.NET, Architecture, Tutorials, Exception handling