Exception Handling

The RuleEngine class has the property CompensationHandlerInfo, which in turn has two properties: CompensationHandler and UserData. The CompensationHandler property is of type RuleEngineCompensationHandler, and the UserData property is of type Object. The definition for the RuleEngineCompensationHandler is as follows:

public delegate bool RuleEngineCompensationHandler(
   Exception ex,
   object userData

The rule engine consumer specifies the handler and the user data to the rule engine by using the CompensationHandlerInfo property of the RuleEngine class. The handler must have the same signature as the RuleEngineCompensationHandler delegate. If there is a run-time exception, the engine calls the handler and passes the exception and the predefined user data as parameters to the handler. If the handler returns true, then the rule engine continues processing. Otherwise, the rule engine aborts processing and returns to the user with the original exception. As you can see, you can use this exception-handling mechanism only if you invoke the policy by using the RuleEngine.Execute method.

If you use the Policy.Execute method to execute a policy, you will need to use a try-catch block to catch any exceptions generated by the rule engine while executing the policy.

If you use the Call Rules shape to execute a policy, use the Catch Exception block for the Scope shape to catch an exception raised by the rule engine while executing the policy.

See Also

  © 2009 Microsoft Corporation. All rights reserved.