Partager via


Specifying Different Handling Actions Based on Exception Type and Policy

The Exception Handling Application Block separates the definition of how an exception should be processed (which is the exception policy) from the application code that uses the block to handle exceptions. You use the configuration tools to create and name policies.

By using exception policies, the application behavior that occurs in response to an exception can be modified without changing the application code.

Configuring Exception Policies

You use the configuration tools to create exception handling policies. An exception policy has a name and is made up of a set of exception types to be processed by that policy. Each exception type has a list of handlers that are executed sequentially.

An application can have multiple policies. This lets different parts of the application handle the same exception types differently. For example, there could be one policy for the top of the exception handling architectural layer that specifies that exceptions should be wrapped by custom data exception types. There could be another policy for the Web user interface layer that specifies that exceptions should be logged, cleansed of any sensitive or unnecessary information, and displayed to the user.

You can also use the configuration tools to rename and delete exception policies. Application code refers to exception policies by name. Therefore, if you delete or rename them, make sure that an application refers to a valid policy name.

Configuring Exception Types

Each exception policy can be configured to handle specific exception types. An application passes the exception to be handled and the name of the policy that should be used to handle the exception to the Exception Handling Application Block. The block looks at the policy to see if it has been configured to handle exceptions of that type. If it finds a match, the block executes the series of exception handlers. The block tries to match the exception type to the most specific type of exception listed in the configuration file. If it does not find a match, the block looks for base class exception types.

In this way, an application can be configured for particular actions for specific exception types (for example, it can wrap or replace exceptions of type System.SecurityException with a custom application exception type), but it can have different actions for more generic exception types (for example, it can log exceptions of type System.Exception). Each exception type can be configured to indicate the action the block will take after the chain of exception handlers runs for that exception type.

Understanding Exception Handlers

With the configuration tools, you can configure which handlers (for example, handlers that log information, wrap an exception, or replace an exception) must run for each exception type. Multiple handlers can also be configured for each exception type. They run in the order they are listed in the configuration file.

Each exception handler in the chain receives the current exception. For the first handler, this is the original exception. This is the exception that the application passes to the block. However, any handler in the chain can change the exception (for example, by wrapping it or replacing it with another exception). Subsequent handlers receive the exception that is returned from the previous handler. If a particular exception type is configured to throw the exception after executing the final handler, the exception that is returned from the last handler is thrown. After you define the exception handling policies for your application, you can modify the application code to apply the policies at the appropriate location. For more information, see Sending an Exception to the Exception Handling Application Block.