. NET 2.0Enterprise Library Exception Handling block in
The Enterprise Library Exception Handling Block provides all the basic code required to handle exceptions. Now, you do not need to write these repetitive Exception Handling codes, you only need to simply use them in the program to ensure consistent and efficient exception handling.
In an ideal program world, all the code you write runs correctly, but the reality is that no matter how careful you are when writing code, errors will always happen, you must have an efficient and configurable framework to handle errors in an "elegant" way. In addition, you must understand that, people usually measure the effectiveness of an exception handling based on the impact on the user experience of the program. Therefore, a good Exception Handling solution not only allows users to handle errors elegantly, but also allows developers or system administrators to handle configurable errors, robust configuration settings are provided, which is also a key component of the exception handling block.
Along with Enterprise Library 2.0, the new exception handling application block has been greatly improved since the release of the old Exception management application block, you can download the EntLib Caching Block from MSDN. For effective use, you must accept the following three main points:
Ø exception handling is a process of handling exceptions when your code detects exceptions.
Ø exception logging is a process of recording an exception, including sending and formatting exceptions to the event recorder or sending an e-mail, while Exception Handling blocks use logging and event logging.
The exception handling policy allows you to control the exception handling and use the behaviors recorded in the external configuration file. The advantage is that you do not need to implement such rules in the Code. In other words, you can define Exception Handling in a policy file, and then modify the behavior during testing, debugging, and product finalization without changing the code to meet different Exception Handling needs.
In addition, when an exception processing block is used, the following three tasks can be performed when an exception is detected:
You can encapsulate exceptions as a new exception and add new context information or error details. When a new exception is passed to the call stack, the original exception can still be accessed through the InnerException attribute.
You can replace the original exception with a new exception. In general, the purpose of doing so is not to let the detailed information of the original exception pass out the program.
You can record exceptions. Of course, you can also use a wrapped or replaced method to achieve this purpose, or you can record the original exception and pass it to the call stack.
Use Exception Handling Block
After installing the Enterprise Library, you can use the exception handling block to write code. to correctly use the exception handling block, follow these steps:
1. Add a pair to Microsoft in your solution. practices. enterpriseLibrary. common. dll and Microsoft. practices. enterpriseLibrary. exceptionHandling. dll Assembly reference, you can use the "add reference" option and go to the X: Program FilesMicrosoft Enterprise Library January 2006bin folder. If you want to use exception processing logs, please add another pair to Microsoft. practices. enterpriseLibrary. exceptionHandling. logging. dll reference.
2. Add necessary items to your app. config (Wi