In this article I'll present a list of best practices for exception handling and I hope that thanks to this you will be able to improve quality of your software.
Of course the less errors the better but even the best programs that were carefully designed and implemented can have some bugs and errors, this is unavoidable. So we should do all that is possible to properly handle these errors when they occur (and they probably will sooner or later). Remember that if you handle exceptions properly then in the future you will have less work with maintaining your software and your client will be more satisfied.
Good and robust software doesn't necessarily mean that it's free of any bugs. It means that it can effectively handle errors when they occur. To design and implement such a software you have to plan exception handling very carefully. In this article I'll present a list of best practices for exception handling and I hope that thanks to this you will be able to improve quality of your software.
Proper use of exceptions
One of the most important things is to properly handle exceptions if they occur. Consider a following piece of code:
Catching an error and then not doing anything about it doesn't make sense because it's not resolving problem. Remember never to put empty code block in catch section.
Avoid exceptions if possible
It's much better to deal with exceptions as early as possible and it's best to avoid them if possible so they never occur. Remember to always validate input data, and check if type is correct. The earlier you detect potential problem the better for you because sometime debugging can be very lengthy process. For example if you're performing some operations on measures such as length or mass you can discard values that are smaller than 0 in the very beginning.
You should use exceptions only for exceptional situations and not for controlling flow of a program. For example if you're checking if a record with a given value exists you should not throw an exception if it doesn't. Use instead simple if statement to check it.
Remember to use finally blocks
The piece of code that is included in finally block is executed in both situations when there is no exception and when there actually is an exception. Use it to clean up your code and close all resources that you use such as for instance database connection. When you're using finally blocks your program is more readable and clean.
Take a look at the example of try - catch - finally:
Consider what you catch
Generally you can use two models for catching exceptions:
- catching all exceptions in one catch block
- creating few catch blocks for different types of exceptions and handling each one of them individually
Here you can see what I mean:
In the first example we handle all errors the same way and in the second one each one is handled separately. Which of these two approaches is better depends on your program. If you handle all errors identically it is of course better to use the first approach, if not use the second one instead.
Remember always to order exceptions in catch blocks from the most specific ones to the least specific to handle them properly.
If it's possible try to use predefined exceptions. Create a new exception type only when it's absolutely necessary - when you do it add Exception in the end of the name of the class like in the following example:
If there is an exception that your program can't deal with, then it should be logged so programmer later can find it and repair a bug that caused it. If you want to learn more about logging check out our previous tutorials. How to use logging in C# .NET projects
C# tip - Use using statement
Using statement ensures that a given resource is properly released. You should use it with all IDisposable objects. Such an object is for example connection to database. After using it you should close it, instead of writing a whole try - catch block you can just take advantage of using statement like in the example below:
Summing up handling exceptions is very important in web design process and you should follow best practices that are listed above to ensure that you do it properly. Devoting some time to handle exceptions the right way can save you a lot of time later when you will be forced to debug your program and find the source of errors.