Common exception handling techniques
Exceptions are expensive in .NET. When an exception occurs in the application, there are resources in place to begin the error handling process when an exception occurs, such as the stack trace process. Even when we are catching and handling the errors, ASP.NET is still creating the Exception
object and everything associated with it along with walking up the call stack to locate the handler.
In this section, we’ll look at common approaches in the industry to minimize the exceptions through “prevention before exception,” why to use logging, why unit testing is similar to exception handling, why empty catch blocks should be avoided, how to simplify exceptions using exception filtering and pattern matching, why blocks are important when releasing resources, and how to rethrow exceptions properly.
Prevention before exception
As we said in the previous section, exceptions interrupt the flow of an application when encountered...