y default, the .NET Framework includes a number of classes that convey information about exceptions generated by the application. Whenever an error occurs in a .NET application, the .NET Framework catches it and raises the associated exception to the application. It is up to the application to catch that exception, process it, and inform users of the error condition in a graceful manner. But not all layers of an application are directly in contact with users. That implies that you might want to have different exception handling strategies for different application layers. Building an effective strategy for dealing with errors in various application layers requires you to write a considerable amount of reusable boilerplate code—which makes exception handling an ideal topic for a reusable library.
To fill that need, the "patterns & practices
" team at Microsoft created the Exception Handling block as an integral component of Enterprise Library 3.0
. Here, you'll see examples of using it with Visual Studio to write robust and fault-tolerant .NET applications.
The Exception Handling Application Block
The Exception Handling block provides a flexible and extensible exception handling mechanism that you can use in all layers of an application. It supports a number of predefined exception handling procedures (also known as policies); these include logging an exception, propagating an exception, wrapping an exception, replacing an exception with another exception, and so on. As a developer, you just need to choose an exception policy, specify it declaratively in your application's configuration file, and Enterprise Library handles all the rest of the work for you.
|Figure 1. Dependencies of the Exception Handling Block: In addition to the core assemblies, the exception handling block references the Logging Block, which in turn depends on the Data Access block if you log to a database.|
After you download and install Enterprise Library 3.1 May 2007
edition, you'll find the Exception Handling block assemblies in the <DriveName>:\Program Files\Microsoft Enterprise Library 3.1—May 2007\Bin
To use the basic features of the Exception Handling block, you need to reference the Microsoft.Practices.EnterpriseLibrary.Common.dll and Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll assemblies. If you need logging capabilities from within your exception management block, you will also need to reference the Microsoft.Practices.EnterpriseLibrary.Logging.dll assembly. Finally, to handle exceptions in Windows Communication Service (WCF)-based services using the Exception Handling block, you have to reference the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.dll assembly as well. Figure 1 shows the dependencies of the Exception Handling block.