The Exception handing applcation block in Enterprise Library 2.0 provides a consistent way for our developers and designers to handle the anomalies at every level of our entire enterprise application, as shown in the following areas:
1, it provides an exception handling method for each layer of the architecture of the application, and is not only limited to the service layer interface;
2. Provide common exception handling methods, such as recording exception information, using our custom exception information to replace the system's exception information and so on;
3, we can define the rules of exception handling, and can not modify the code to modify the exception handling rules;
4, it supports our developers to create their own exception handling methods;
5, the same method to invoke the exception handling method, which makes exception handling can be used anywhere in the application.
When we do exception handling, we generally adopt a strategy:
1, packaging
2. Replace
3, log
So, Exception handing application block can give us the benefit of development? First look at the following code:
DataSet customersDataSet;
try
{
customersDataSet = RunQuery("GetAllCustomers");
}
catch(Exception ex)
{
string formattedInfo = FormatException(ex);
Logging.Log(formattedInfo);
throw new DataAccessException("Database access failure for query GetAllCustomers",e);
}
The code above estimates that we are the most familiar, yes, such code in our application is expected to run through every corner of our program, but when we change the way we handle the exception, we have to modify all the code, which is a very depressing thing, Exception The appearance of handing application block has solved the problem for us, after using the exception handing application block, we only need to modify the configuration file to change all the exception handling methods in the application. As follows:
DataSet customersDataSet;
try
{
customersDataSet = RunQuery("GetAllCustomers");
}
catch(Exception ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "Data Access Policy");
if (rethrow)
throw;
}