Enterprise Library In-depth analysis and flexible application (4): Create a custom exception Handl

Source: Internet
Author: User
Tags exception handling

Enterprise Library In-depth analysis and flexible application (4): Create a custom exception handler change Elab exception handling mechanism

1. Background and motivation

Microsoft Enterprise Library ELAB (Exception Handling Application Block) provides a policy-based (Policy) approach to exception handling, in different contexts, such as multiple layers of architecture, We can define different exception handling strategies. For Elab, the Exception handling Policy = Exception type + Exception Handler (s), which means that the exception handling policy prescribes the need for some type of type to pass through those Exception Handler to deal with.

In this sense, the Elab exception handling mechanism is based on the exception type, and the type of exception is the minimum granularity of the exception we handle. For a certain exception handling strategy, the same type of exception thrown on different occasions will be handled using the same exception handler. For example, a SQL Server based data access operation throws SqlException for database connection failures and violation of data consistency constraints. Both of these cases are equivalent to Elab because it can only handle exceptions based on the type of exception.

In many cases, this exception handling based on the exception type level does not solve the problem of our actual exception handling. We often need more granular exception handling mechanism--for the same type of exception thrown, according to the specific property value of the exception object for the corresponding exception handling.

2, from the type-based exception processing to the attribute value exception handling

The goal we need to realize is that we can handle the thrown exception by assigning the corresponding exception handler to the specific value of the attribute. Because Elab based on abnormal type of exception handler distribution mechanism, we can not change, we can only use a number of alternative mechanisms to achieve "curve salvation", to achieve our distribution based on attributes exception handler.

The specific implementation scenario is to create a special Exception Handler that Exception Handler the corresponding Exception Handler based on the value of an attribute of the exception object. For this particular exception handler, he implements filtering based on attribute values, which we name as Filterableexceptionhandler.

In general, the type of exception and the corresponding exception handler are directly matched by the form below. When the fooexception was thrown, two exception Handler,exceptionhandlera and Exceptionhandlerb were successively executed.

When Filterableexceptionhandler is introduced, the entire structure becomes one of the following: Filterableexceptionhandler is assigned to Fooexception, and when Fooexception is thrown, Filterableexceptionhandler is executed. The Filterableexceptionhandler itself does not perform exception handling-related logic, and it works by creating a corresponding Exceptionhandler (s) based on the value of a exception property and using them to handle the exception. As shown in the following figure, when exception. Property=value1 is the creation of Exceptionhandlera and exceptionhandlerb processing exceptions, when exception. Property=value2, the real creation of exception handling is EXCEPTIONHANDLERC and exceptionhandlerd.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.