Utilize the Exception Handling Block in .NET-based apps
Takeaway: Discover how the Microsoft Enterprise Library's Exception Handling Block configuration program makes it easy to set up exception policies that are used in application code.
As a developer, my time is limited, so anything that can save me time is greatly appreciated. For this reason, I am a big fan of Microsoft's Enterprise Library. After all, who can pass up free code? I covered logging via the Enterprise Library in a previous column, but today the focus is on handling exceptions with the Exception Handling Block.
Exception handling
Exception handling is a great feature of the .NET Framework that allows you to gracefully handle runtime problems in a .NET-based application. After all, it is much better to avoid the default error messages generated by .NET (there is no need to confuse users). In addition, the default error messages can be security holes where sensitive information or code is displayed when/if problems arise. Often, error messages reveal code details that are meaningless to users but may be utilized by malicious users.
Exception handling is repetitive; that is, the same approach to handling exceptions is often used over and over again. There are patterns to handling common exceptions. With this in mind, the Exception Handling Block was developed to provide a consistent way to deal with code exceptions.
Enterprise Library
The Enterprise Library was built and tested by Microsoft using best practices. It allows you to utilize consistent exception handling in your applications. The exception handling can be as simple or complex as required by a project. It includes a configuration tool to define exception policies that specify how exceptions are handled (specific exception types may be handled differently).
The library includes some basic actions with installation. This includes logging, wrapping an exception with another exception type, and replacing an exception with another. It also allows you to create custom actions. All of these options are set up via the Enterprise Library configuration tool (which is installed with the library). Before using the tool to configure exception handling for a specific application, you must set up the application.
Using the Exception Handling Block
The first step in using the Exception Handling Block is to add a reference to its dynamic link library (DLL) in the project. This is no different than adding other DLLs—simply select Add Reference and locate the DLL on the system. Once the reference has been added, you include it within a code file with the following reference in C#:
usingMicrosoft.Practices.EnterpriseLibrary.ExceptionHandling;
Here's the syntax for VB.NET:
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Once you add the reference to the application, the configuration tool is used to add/configure exception policies for the application. So, open the configuration tool and select Open Application from the File menu. Locate the config file for your application (app.config for Windows application, web.config for ASP.NET, and so forth). The configuration information for the block is added and maintained in the application's config file.
As an example, I have an ASP.NET application. I configure exception policies to handle generic exceptions, as well as casting and null reference exceptions. This information is maintained via the configuration program, but entries are added to the application's configuration file (web.config in this case since it is a Web application). The code in Listing A is added to the web.config file.
The first line is the main node—enterpriselibrary.configurationSettings—and it includes the applicationName attribute that corresponds to the name assigned in the configuration application. This tells the system what exception handling policies to use. Once everything is set up and you have added one or more exception policies, it may be utilized in your code. The simple C# try/catch block in Listing B catches an exception and uses an if statement to determine if the generated exception is of the type defined in the policy specified (Policy Name parameter). (Listing C contains the equivalent VB.NET code.)
Notice that only a throw statement is used if the exception matches the policy definition. Microsoft says it is very important to just use the throw statement rather than throw ex. If you have "throw ex," then the stack trace of the exception will be replaced with a stack trace starting at the re-throw point, which is usually not the desired effect.
At this point, you have taken advantage of the Exception Handling Block. Of course, it does require installation and setup/configuration before you may use it in an application.
The Exception Handling Block provides plenty of options, but it may be overkill for certain applications. This means that it doesn't necessarily replace every instance of exception handling in an application.
Conclusion
The Exception Handling Block moves beyond previous iterations that provided simple logging. It combines exception management with exception policies to provide an overall strategy for exception management. Using these policies, exceptions may be caught, thrown, rethrown, or even ignored. The configuration application allows you to easily set up policies that are utilized with little code in your projects.
Miss a column?
Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.
SponsoredWhite Papers, Webcasts, and Downloads
- Improving Decision Making Through Enterprise Information Management SAP
- Live Webcast: Mobile Tools for Competitive Advantage PC Connection
- Accelerating the Next Phase of Virtualization Riverbed
- Accelerating Network-based Backup Riverbed
- Creating Business Value Through Process Integration and Composition SAP
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
